Android java.lang.NoClassDefFoundError after import JAR - java

I got a problem...
I made a Jar (called "GeoFence.jar"), that I have to use on a Android Project.
So, what I done is:
Look In so many webs HowTo's
create "libs" folder in the android project.
add my JAR (manually)
then I went to Java Build Path and checked that my JAR was included to the Android Dependences.
finally I made the necessary Imports to use the class I need from JAR.
After do all of this, I runned my App, and it throws this error:
and I don't understand what's going on, cause, I Fixed the Project Properties and cleaned it. And it's still not working.
Here is how i have the structure and the build path.

for adding jar use below two stpe: but for you just do 2nd.
1. Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
2. Your Project -> right click -> Properties -> Java Build Path -> Order & Export --> Select All --> OK
After taht just clean your Project and RUN.

If you go to you Android Dependencies folder, you should be able to expand the GeoFence.jar file. This way, you should be able to find the class com.elias.polygon.City if you keep expanding subfolders.
If you CANNOT see it, you probably made an error in exporting the jar file.
To solve the export see this question on so
If you CAN see the class, you have an error in your build path/ project properties
To solve the project setup, see this question

the problem that I had, was that the jar I wanted to import, used some packages that in android are not "allowed" so I had to do in a different way.
After that, it happens the same, but the problem was that the Jar was Build in v1.7.
When I builded in v1.6 android accepted it.
So, first of all look for:
Don't use packacges that in android are not allowed
Look in which version is builded the solution.

Related

NoClassDefFoundError in nested jar (Android)

I have created simple app to test the scenario which is failing with NoClassDefFoundError.
Lets take for example i have Test1 android project with Class TestClass with some methods.
This Test1 project i exported as a test1.jar and exported only source folder with Class TestClass and classpath and .project files.
In another android project Test2 i added this test1.jar in my libs folder. And in this Test2 project i have a class Test2Class which calls methods of test1.jar class.
After this i exported this Test2 project as test2.jar file following above steps.
So when i use this test2.jar in another project i get this above error NoClassDefFoundError. It is the scenario of jar inside a jar.
Is anywhere should i do something so i am able to access a jar inside a jar.
Thanks in advance.
Try this Step by step:
1. Remove all library projects then Clean.
2. Go to first Project Test1 Right click Properties -> Android. Check Is Library(make it library).
3. Go to second Project Test2 Right click Properties -> Android in the Library section select Add -> Add first project as library.
4. Go to second Project Test2 Right click Properties -> Android. Also, Check Is Library.
5. Go to third project C Right click Properties -> Android. In the Library section select Add -> Add First and Second as library. (Do not make Proj C as library)
6. Now Clean everything and run Proj C.
Edit:
You can also see this bug Bug 405212 - Do not pack jars bundled inside a packed jar and Bug 361628 - Nested jars cannot be unpacked with Java 1.7. Try the above way it should work.
Remove the jars from the java build path. Add new folder name as "libs" and posted all the jars in that folder. Application will run fine
Try this :
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it.
Mark all checkboxes and Click on Apply and clean the project.
It cant be done. You will need to unzip the JARs and add them to the classpath to expose the nested JAR. See this Java bug for reference.
There are several alternatives to handling it in a proper way, see this question for example, which recommends One-jar, Uber jar or Shade
Note: This is relevant for Java, Andoid might behave different, but it seems like it has the same issue.

Import Java Project, not found src

let me tell you that I'm new in Java and I'm having this problem with an Android App.
The app is made by two projects, the launcher activity(Project A) and another one (Project B) which has some common SRCs for multiple Android Apps (like http connection classes, etc).
I imported project B from A, and in my .java files of my project A, I could import without problem src from B (import path from B;)
But when I'm debbuging and it has to use a file from B, I get an NoClassDefFoundError with the path of some file in project B.
After that, my app fails and closes.
Sorry if my English is complicated. Hope you understand my problem and can help me.
I guess it's something about a missing step in my import.
Importing just puts these projects into your workspace. If they are 2 separate projects you should import them as separate projects. Either right click import and navigate to them on your file system or if they are already in your workspace, do create new java project and give it the same name as the directory of the project (The wizard will make it clear that it recognizes a java project exists in that folder).
Then check to make sure that the package names and imports one package to the other are correct. If they weren't you should errors marked on those lines.
Finally make sure that the project which you are actually running from has the other project on its build path. Right click on the project, configure build path, and add the packages. Alternatively you can use something like maven to automate the installation of this second package as a jar which will automatically update in project A when project B is changed)
Please make sure you include the classes/src from project B in your project A's build path , by right clicking on your project A "Build Path" -> "Configure Build Path". Eclipse automatically will add those classes to your class path too.
The NoClassDefFoundError usually occurs when the classes needed to compile the project are present during build time but missing during run time.
Hope this helps !!
To reference other projects source in current project, just import them into your current project by adding them to your workspace. This link has some neat steps to accomplish this. You can follow which one suites you.

Android external Jar

I have some api with java and I need it in my android app.
When I am using it with including all packages and classes it works fine. Now I create JAR file from my java project using export. Now when I add the jar to my android app as a external jar my all errors are disappeared but when I run my app I got an error
java.lang.NoClassDefFoundError.
I have seen that kind of errors early in my apps and it was the problem of Jar. How can I generate JAR file to avoid this situation?
You have probably built that jar file with jdk 1.7. Classes compiled with jdk 1.7 are not included in apk file. Try to change java compiler to 1.6 before you export jar. In eclipse go to Window -> Preferences -> Java -> Compiler.
copy the jar file in the libs folder of your android project.
After that you may want to clean your project and rebuild it. You can even clear manually the bin dir content.
You have to add your jar file to your project's buildpath.
To make sure it's exported in your APK, you also have to check it under the Order and Export tab, in the build path preferences of your project.
A better approach might be the following:
remove the current exported jar from your project
select the project you want to import, right click -> properties -> android -> check 'mark as library'
right click your current project -> properties -> android -> locate the 'add' button in the bottom. Click it and select the library project.
Using this approach any changes in the library project will be reflected in your current project (at least after doing a clean projects).
I have done it !!! Thanks everybody.
So the solution is :
1. change java compiler to 1.6 before you export jar (As #Lecho mentioned)
2. Move the Jar file to lib folder
So it works !!! thanks everybody

IllegalArgumentException error after added google jar

I get this error
java.lang.IllegalArgumentException: already added: Lcom/google/protobuf/AbstractMessageLite$Builder$LimitedInputStream;
and at the bottom of the Log,
Conversion to Dalvik format failed with error 1
after I added the 'libs' folder that includes jar files for google map/place that I'm trying to implement to an existing package.
I've done some research and found a solution which that
Project » Properties » Java Build Path » Libraries and remove all except the "Android X.Y"
but that's not in my case since I can't remove them from the list.(I can remove the Android Dependencies folder but i'm not sure if i should do that)
This issue is that you've got two jar files with duplicate classes. Try removing protobuf, if everything still compiles happily then hey presto you're in luck.
Otherwise, one workaround I found here is:
Add the library project using project properties -> android - add library project.
Add the linked src files to your build path as well.
Build the project. You should see the class files from you library project in the bin folder.
After that, REMOVE the linked src files from the library project in your application >project.
Run your application.
If you clean the project, then repeat these steps from step 2.

eclipse doesn't compile the project

I had running project opened in eclipse. After an accidental restart of windows, now when I open the eclipse I see my project is marked with a little red cross. Now when I run the main method I get a java.lang.NoClassDefFoundError.
I have tried restarting eclipse, Project -> Clean but it doesn't solve the problem.
When I checked the project directory, inside 'target' folder there are no compiled .class files. I tried building the project but I can't get the compiled class files, which is the reason for the error.
How do I solve this?
I tried all the solutions here, but here's what worked for me.
First, remove the project from eclipse.
Then, in your project folder, delete the 2 files that eclipse creates. They're hidden, but they're called .project and .classpath.
Finally, add the project back into eclipse and eclipse will recreate those 2 files and a new configuration for your project.
My specific issue was that it a project that was missing a Java Builder because it thought it was a scala project. The only way for me to reset this was by removing the project, deleting the 2 files, and adding it back in.
Maybe Eclipse's workspace files have become corrupted. Restart Eclipse and choose a new workspace folder (or choose Switch workspace from the menu). Then import the project files into a new project.
Try deleting the run configuration and remaking.
If that doesn't work, open the class, ctrl+f11 to run. That will force Eclipse to update its classpaths & rebuild its main indexes.
To delete the configuration:
- Menu -> Run -> Run Configurations.
- Select your configuration
- Press delete or use the delete icon
If you are using maven, this will likely fix it: Right click your project -> Maven -> Update project.. (or alt + F5), select your project, make sure "Refresh workspace resources from local filesystem" is checked and click ok.
My eclipse project has similar issue though the error appeared slightly different. I imported a third party java project into Eclipse, with Project > Build Automatically checked, do a Project > Clean..., no classes were generated in the output bin folder. Tried different options, no luck.
I noticed a red exclamation mark at the top left corner of the project name.
Right click on project name, Properties > Java Build path, I saw "Build path entry is missing: cci/src". Manually created src folder, the red exclamation mark gone. Do a Project > clean..., project compilation successful
P.S. The eclipse project is not properly organized, there are no code under src folder. I suspected src folder went missing while checked in by original author since there are no code there.
Was having the same problem and finally found the glorious hint here.
In my project there is one *.scala File and Eclipse Neon is incompatible with the Scala IDE, thus there is no builder for scala and the whole project is not built.
After removing the Scala file and the Scala Package from the linked Libraries )Project Properties --> Build Path --> Libraries) and afterward re-adding the Java Builder (no idea why this one went missing, but here's a useful help on how to add the Java builder when missing), the project could be built and run again.
Try refreshing and then cleaning the project.
For me the problem is in missing jars,the jars were added first and the location of those jars where changed after.
So When I correctly added all the required jars again to the build path, it starts to build project correctly.

Categories

Resources