How to use android library in Java general desktop app?
I want to use some NON-GUI classes of android.jar like android.net(*), in my desktop application. How to do that? I am only being able to use them in android applications till now by using "import android.net.*;", but in desktop, it's an error
2/25/2020 3:57:22 PMSarthak 🇳🇵
6 AnswersNew Answer
You need to add the jar to the build path and import the classes then. I'm not sure if it will run tho. Depends on the classes and their dependencies. If you elaborate on what you're trying to achieve maybe someone can provide a 'pure' Java solution.
Tashi N When I compile the project including android.jar in classpath, it compiles successfully. But when I run the project, it errors "java.lang.NoClassDefFoundError": android/net/LibraryName$Inner ...
And the .class file which causes the error is on the build path? To be more precise this class is contained in the jar you added to the build path?
Tashi N The folder structure is as below: project/ main.java libs/ android.jar In main.java, I've used some classes of android.jar, I compile project using command javac -cp ".;libs/android.jar" main.java And it compiles successfully. Then I run by command: java main Then it throws exception/error that I mentioned before
I wouldn't recommend to simply copy the jar to some folder, but well. It compiles :D Are you sure that the jar contains the missing .class file? The class you imported might have more (external) dependencies which might not be evaluated at compile time? Also, is your classpath setup correctly (is your libs folder on the classpath?).
Tashi N While compiling, class path is correct. While running binary( class), setting class path doesn't seem compulsory because I've already included jar file (classes archive) while compiling.