如何为java项目生成jar文件并在android项目中使用jar

时间:2023-02-05 14:11:04

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

我正在尝试为java项目生成一个jar文件,然后在android项目中引用jar文件。作为测试,我创建了一个只包含以下类的简单java项目

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

我在Eclipse下导出这个项目一个可执行的Jar文件。然后我将这个jar文件复制到android项目的“libs”文件夹中,因此jar文件出现在私有库中,我在“Order and Export”选项卡下选中私有库的框。我通过在android项目的主要活动的onCreate()中添加以下语句来引用jar文件

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

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

但是,当我运行Android项目时,我得到了

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.如何为java项目生成jar文件并在android项目中使用jar如何为java项目生成jar文件并在android项目中使用jar

附件是订单和导出选项卡以及每个库中包含的jar的屏幕截图。

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.

我在这个网站上看过几篇关于NoClassDefFoundError的帖子(例如这个),并尝试了评论或答案中列出的解决方案。但在这个简单的场景中没有人能解决我的问题。

4 个解决方案

#1


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

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

#2


0  

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

您可以通过以下步骤来完成,这是从android库项目制作jar

$ cd your_project_path
$ android update project -p .
$ ant jar

#3


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.

为什么不将Java项目导入Android工作区,然后将其用作Android项目中的库?这应该可以解决您的问题。

#4


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.

我不认为Dalvik VM可以运行Java字节码。此外,您的Java JAR不包含任何.dex文件,其中的类将无法找到。

See this answer for info on DEX format

有关DEX格式的信息,请参阅此答案

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

您可以将Java JAR转换为Android JAR。或者将Java源代码添加到Android项目中,甚至引用与Java项目相同的源文件......

#1


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

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

#2


0  

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

您可以通过以下步骤来完成,这是从android库项目制作jar

$ cd your_project_path
$ android update project -p .
$ ant jar

#3


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.

为什么不将Java项目导入Android工作区,然后将其用作Android项目中的库?这应该可以解决您的问题。

#4


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.

我不认为Dalvik VM可以运行Java字节码。此外,您的Java JAR不包含任何.dex文件,其中的类将无法找到。

See this answer for info on DEX format

有关DEX格式的信息,请参阅此答案

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

您可以将Java JAR转换为Android JAR。或者将Java源代码添加到Android项目中,甚至引用与Java项目相同的源文件......