4

I am trying to generate a jar file for a java project and then references the jar file in an android project. As a test, I create a simple java project only containing the following class

package test;
public class Test {
public static void hello(){
    System.out.println();
}
public static void main(String[] args){
    hello();
}

}

I export this project an executable Jar file under Eclipse. Then I copy this jar file into the "libs" folder of an android project, so the jar file occurs in the private library and I check the box of private libary under the "Order and Export" tab. I reference the jar file by adding the following statement into the onCreate() of the main activity of the android project

  Test.hello(); //reference the jar file

However, when I run the Android project, I get the

java.lang.NoClassDefFoundError: test.Test

error. The full error track trace is below

 10-17 23:14:52.976: E/AndroidRuntime(16725): FATAL EXCEPTION: main
 10-17 23:14:52.976: E/AndroidRuntime(16725): java.lang.NoClassDefFoundError: test.Test
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at      
  com.skyhookwireless.samples.wpsapitest.WpsApiTest.onCreate(WpsApiTest.java:71)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at    
  android.app.Activity.performCreate(Activity.java:4470)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.app.ActivityThread.access$600(ActivityThread.java:128)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
 android.os.Handler.dispatchMessage(Handler.java:99)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at 
  android.os.Looper.loop(Looper.java:137)
  10-17 23:14:52.976: E/AndroidRuntime(16725):  at     
   android.app.ActivityThread.main(ActivityThread.java:4517)
    10-17 23:14:52.976: E/AndroidRuntime(16725):    at  
    java.lang.reflect.Method.invokeNative(Native Method)
   10-17 23:14:52.976: E/AndroidRuntime(16725):     at 
  java.lang.reflect.Method.invoke(Method.java:511)
  10-17 23:14:52.976: E/AndroidRuntime(16725):  at          
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
  10-17 23:14:52.976: E/AndroidRuntime(16725):  at  
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
 10-17 23:14:52.976: E/AndroidRuntime(16725):   at    
    dalvik.system.NativeStart.main(Native Method)

Attached are the screenshots of the order and export tab and the jars contained in each library.the jars contained in each libraryorder and export tab

Does anyone know how I can correct this?

I've read couple of posts (e.g. this) on this site regarding the NoClassDefFoundError and tried the solutions listed in comments or answers. But no one solves my problem in this simple scenario.

Community
  • 1
  • 1
sma
  • 877
  • 2
  • 9
  • 24

4 Answers4

0

I feel the main activity of your project and test class are being loaded by separate classloaders. Take a look at this to figure out where you might be missing it.

http://javareferencegv.blogspot.com/2013/10/debugging-javalangnoclassdeffounderror.html

Gaurav Varma
  • 1,939
  • 4
  • 18
  • 36
0

You can do it by following steps , this is for making jar from android library project

$ cd your_project_path
$ android update project -p .
$ ant jar
ayon
  • 2,150
  • 2
  • 16
  • 31
0

Why don't you import your Java project into Android workspace and then use it as a library in your Android project? This should resolve your issue.

VikramV
  • 1,061
  • 2
  • 11
  • 27
0

I do not think it is possible for the Dalvik VM to run Java bytecode. And also your Java JAR does not contain any .dex files, the classes inside it would not be found.

See this answer for info on DEX format

You could convert your Java JAR into an Android JAR. Or add your Java sources into your Android project, even referencing the same source files as your Java project...

Community
  • 1
  • 1
Usagi Miyamoto
  • 5,721
  • 1
  • 16
  • 28