android can't find class from external jar
Solution 1
There's no need to build the external jar. Try removing it from your build path and follow these steps:
- Create a folder in the root of your Android project called
libs
. - Add the jar to that folder.
- Right-click the jar and click to add to build path.
- Clean your project and try it again.
Solution 2
i. Generate Jar for your external Classes
ii. Copy to Libs folder of your Android Project
iii. Open Eclipse->Right Click on Jar->Add To Build Path.
iv. Clean Project and Run Application.
Related videos on Youtube
Comments
-
I Z almost 2 years
I am trying to create a simple test app that basically extends the Android Hello World tutorial app by invoking some simple functionality from an external JAR. However, when I run the app, it can't find the class from the JAR. What am I doing wrong?
Here's entire source of the JAR:
package com.mytests.pow; public class power2 { private double d; public power2() { d = 0.0; } public String stp2(double dd) { d = dd*dd; return String.format("%e", d); } }
And here's the "Hello World ++" source:
package com.myLuceneTests.namespace; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import com.mytests.pow.*; public class AndroidSimpleSIH_EclipseActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { power2 pp = new power2(); String iout = pp.stp2(12.0); super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText(iout); setContentView(tv); } }
When I run the app, I get this in logcat:
11-22 12:24:52.697 2963 2963 E dalvikvm: Could not find class 'com.mytests.pow.power2', referenced from method com.myLuceneTests.namespace
.AndroidSimpleSIH_EclipseActivity.onCreate
and then
11-22 12:24:52.713 2963 2963 E AndroidRuntime: java.lang.NoClassDefFoundError: com.mytests.pow.power2
What am I doing wrong? Thanks!
By the way, my actual goal is to use a real JAR (rather than this toy one) in an Android app. I might have access to the code for that JAR and might be able to rebuild it but it's a big piece of Java code and I am likely to encounter problems when rebuilding it so I am trying to use the pre-built JAR.
-
slayton over 12 yearsWhy would the compiler care about the capitalization of class names? Its simply a convention that java programmers have adopted to make their code more readable
-
LuxuryMode over 12 years@slayton You're right. I assume the external lib is following convention, in which case power2 isn't the right way to reference that class.
-
I Z over 12 yearsChanging case did not change anything. What I am trying to do now is to build the external jar in Netbeans instead of Eclipse. Recently I noticed that using NB-built JARs in Eclipse behaves differently form using Eclipse-built ones. Will report on what I find.
-
devunwired over 12 years+1 Because the answer is embedded here. You must include any JARs you want packaged in the libs/ directory of your project so they get bundled into the APK. Adding them to the build path just lets Eclipse pass the compile step.
-
I Z over 12 yearsUPDATE: The proposed solution did NOT work. What did work was rebuilding the JAR in NetBeans. Specifically, I created a class project Power2NB in NB that had the same content as the original Power2. I then built its JAR and added it as "external JAR" to the Android project in Eclipse the way I was originally adding the Eclipse-built JAR for Power2. The app worked fine. This means that the structure of NB-produced JAR is "better" in some way. I don't know yet what this means exactly. I will be looking into it, but in the meantime... Any ideas? Thanks
-
Anna Billstrom almost 12 yearsThis saved me- I had done some stuff to muck up my Eclipse and the simple series here
create a folder in the root of your Android project called libs. Add the jar to that folder. right-click the jar and click to add to build path. Clean your project and try it again.
was spot on - thanks @LuxuryMode -
Hong almost 12 years@Devunwired "Adding them to the build path just lets Eclipse pass the compile step". This seems to explain the solution that solve the problems including mine. What has puzzled me is that I have added some external jars without including them in the folder libs, and they work. I have no clue why some have to be added to libs, and some do not have to.