+ 1

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

25th Feb 2020, 3:57 PM
Sarthak
Sarthak - avatar
6 Respostas
+ 1
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.
25th Feb 2020, 6:17 PM
Tashi N
Tashi N - avatar
+ 1
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 ...
25th Feb 2020, 6:39 PM
Sarthak
Sarthak - avatar
+ 1
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?
25th Feb 2020, 6:46 PM
Tashi N
Tashi N - avatar
+ 1
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
25th Feb 2020, 6:52 PM
Sarthak
Sarthak - avatar
+ 1
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?).
25th Feb 2020, 6:59 PM
Tashi N
Tashi N - avatar
+ 1
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.
25th Feb 2020, 7:10 PM
Sarthak
Sarthak - avatar