eclipse / android / java - cant reference class from jar file in libs - java

OK, so I made a jar file, using the eclipse export feature, on one of my projects, let's call it X.
So X.jar has a single class in it, M.class.
I took this jar and put it into the libs/ folder of another project.
When I do
M m = new M()
it tells me it can't find M, and offers to import it from the package it's in. When I accept that import, it tells me the import cannot be found.
Now, what's confusing (or at least frustrating) is that if I export the jar with M.java instead of M.class, it works - but I need the jar to not be recompiled in the new project, hence why I was hoping to use the class file and not the source.

Instead of placing the jar file in the lib folder, try adding it to the build path.
In eclipse:
right click on the project -> Build Path -> Configure Build path... -> Add External JARS
In IntelliJ:
Ctrl+Alt+Shift+S -> Libraries -> new(Plus Sign)

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.

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

Android java.lang.NoClassDefFoundError after import JAR

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.

how to import own jar into android project (ADT 20)

If I import a selfmade jar file into my Android project (add it to the libs folder) I get an "Could not find class" error sent by dalvikvm. (I'm using Eclipse)
Obviously it's not an import error, because downloaded jars (like jdome) worke fine.
So I guess it's an export error. The way how I export a jar is: right click on project -> export -> jar -> leave default settings -> finish
I'm sure it's no internal problem, because the jar contains only one class which has only one method for test reasons.
Does anyone know what I did wrong?????
Just copy and paste your .jar file in libs folder of your project.
Yry this, I hope this will work for you:
Go To Your Project
Right Click
Properties
Java Build Path
Libraries
Add External Jar
Select Jar File
OK

how to make a single jar file of multiple classes that can be used in other projects

I have some classes say I have 20 classes with different objectives. Now what I want to do is that I want to make a single jar file of all of them. I want to do this because I want to use these classes as a single Jar in other projects. How can I do this?
You can also do it using Command Line. Transfer all the classes into a folder. Say it is in the folder with absolute path C:\Java\Project1.
Go to the same folder from command prompt. And execute the following code :
jar cfv Project1.jar *
Here c will create jar, f will transfer the content into the file provided i.e. Project1.jar and v will print the process done into the console.
The Wildcard * will take all the files present in the folder. And the Jar file will be created in the same folder.
Now, you can copy this jar anywhere you want to use. :)
If you are using Eclipse
You can do this by,
Right Click on the Package -> Export -> java -> jar file
Package means any Folder Package or Any Group of Selected JAVA files
Just package the classes as normal!
i.e. From the command line: jar cf jar-file <input-file(s)>
More info can be found here:
http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
There's already a good discussion on merging multiple jars using ANT here, if you want it as part of a build process:
Clean way to combine multiple jars? Preferably using Ant
Or you can just put the classes in one eclipse project and reference that project in the other android eclipse projects.
Right click on android project => Build Path => Configure BuildPath =>
Projects => Add
This will not work when you use ant to build your projects.

Categories

Resources