How to use android library in Java general desktop app? | SoloLearn: Learn to code for FREE!

+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

2/25/2020 3:57:22 PM

Sarthak 🇳🇵

6 Answers

New Answer

+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.

+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 ...

+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?

+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

+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?).

+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.