Android external Jar - java

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

Related

How to add referenced library JAR files using relative path for eclipse Java project in vscode

I checked this question and many other posts to help me resolve my issue, but nothing worked.
I trying to open an existing Java Project from Eclipse in vscode. The project was opened successfully in Eclipse 2021-06 and I fixed the referenced libraries by selecting the JAR libraries under lib/ folder and right-click - configure build batch - add select add to build path. Then the file .classpath was updated automatically. Then I had to download JDK 11 (needed for vscode) and JDK 1.8 needed for the Java project. I managed to work with eclipse to set the JDK correctly and all worked fine. I was able to compile the project, export the result JAR and deploy and all worked well.
I am having trouble to open this project in vscode using Open Java Project option. I am facing issues adding referenced libraries this Java project as it is a managed project (since it was created in Eclipse). When I try to add the libraries using vscode UI, I am getting mixed results and sometimes it is not working and the build fails. I was surprised as in some instances, things just work, and then they don't.
I tried to update .classpath file in vscode and still same issues. When I try to add the libraries from Referenced Libraries (Read Only) under JAVA PROJECTS tree in vscode by clicking +, it has no effect. This step will modify the file settings.json under .vscode folder by adding the relevant values to the property "java.project.referencedLibraries".
I am confused about how to configure the referenced libraries for Java projects in vscode. Following is a summary of questions:
How to add a library using a relative path to a Java Project based on the project root folder?
What is the difference between adding a library using the .classpath file and by modifying settings.json file? Which one will win?
How to add a folder with all JAR files to be as the referenced library to the Java Project? This is to avoid adding one file at a time.
Why the .classpath and .project files are not showing in vscode explorer view? It will only show when you open the file in vscode from the Windows File Explorer.
I found command Configure Classpath but it is read-only, which I think it is the same as the .classpath file. Is there a way to change it from vscode UI?
When I remove .classpath file or when I removed the classpathentry lines from the .classpath file, the node 'Referenced Libraries (Read Only)' under JAVA PROJECTS in vscode view was removed. Why?
See the snapshots below for more details.
I appreciate your help.
Basically speaking, java extension looks for jars from Referenced Libraries.
Here're my answers to your questions:
Right click the jar and choose copy relative path then add it to settings, click the refresh button then the added jar should be displayed under the option Referenced Libraries:
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When it comes to add jars, settings java.project.referencedLibraries wins.
No way to select
jar folders but you can use keyboard shortcuts to select all jars then add them.
There's a setting called "java.configuration.checkProjectSettingsExclusions", and it's true by default, so .project and .classpath won't be shown in VS Code.
The command Java: Configure Classpath works on my machine, which can customize current project.
My guess is when you open a new project, vscode popped up a window and ask you if trust it, and you chose Not, then project is read-only. Trusting the workspace then try the command Java: Configure Classpath again, it should be writeable.
The answer by #Molly Want-MSFT helped me a lot. Following is what I did to resolve the problem for good. I applied the steps below many time to verify they will work every time.
Steps to open a Java Project in both Eclipse and vscode:
Download the JDK needed for vscode, Eclipse and your Java Project.
Import the project in Eclipse and setup the JDK for the workspace and the project. Also, make sure to setup the compile level to match the JDK.
Set up the Project JDK to match the default of the workspace.
Build the project (Project-Cleanup) and Export the JAR to make sure all is OK.
Now open the project in vscode.
Setup java.home in Settings, in user and workspace sections. This must be JDK-11 or higher to allow vscode to function properly for Java Projects.
Add references to the installed JDKs in User's settings.json under "java.configuration.runtimes" section.
Restart vscode and take the option Configure Java Runtime from JAVA PROJECTS view. Make sure that the JDK of the Java Project is detected and working correctly.
You may have to open one of the Java Source Code Files. Wait a bit until it will settle down. Check the Java Build Status progress by clicking the spinning icon in the bottom right. This icon is for Language Server and it will turn into an icon that looks like thumbs-up when build/compile is done.
Close and open Configure Java Runtime to verify that the JDK was detected by vscode.
Ensure that both JRE System Libraries and Referenced Libraries under JAVA PROJECTS view are visible without any errors. Check the Problems view and try to resolve all errors.
The Referenced Libraries should be Read Only because this project was setup in Eclipse. You can delete the .classpath and .project files and open the project again in vscode, and try to fix the problems by adding libraries using the + button. When such files are deleted, the project will become Unmanaged. Later, you can restore such files.
When the project is unmanaged, you use Configure Java Runtime from JAVA PROJECTS view and you can check the project type. You can change the JDK to one of the installed ones as per the section "java.configuration.runtimes" in the user's setting.json.
If the the Referenced Libraries is read-only, it has no effect even if you can add libraries into settings.json, but the .classpath file will win.
From JAVA PROJECTS view, you can use the option Build Workspace and Clean Workspace to troubleshoot and try to resolve errors.
Finally, you can Export JAR from the option on JAVA PROJECTS view. This option is not clear and it looks like and arrow pointing to the right |-->.
I hope this helps, and if you have any question, please post a comment and I will try to answer back when possible.
I just change the path strings of the jars in the .classpath file and it works for me.

Automatically load libraries on project creating. Android, Eclipse

I have figured out how to obtain the effect i desire on the desktop.
(Window > Preferences > Java > Installed JREs > jre7 [Edit] > [Add External JARs])
but i cant get the same effect on android.
In the desktop project i can see the folder "JRE Sysytem Library(jre7)" contains all the JARs i referenced
This directory is not present in the android project. How do I add libraries to be automatically included in an Android project without needing to use the "R-Click Project > Properties > Java Build Path > Libraries" method OR the "R-Click JAR file > Build Path > Add to build path" method in the IDE "Eclipse"
In the desktop project i can see the folder "JRE Sysytem Library(jre7)" contains all the JARs i referenced
An Android app does not use the JRE.
How do I add libraries to be automatically included in an Android project without needing to use the "R-Click Project > Properties > Java Build Path > Libraries" method OR the "R-Click JAR file > Build Path > Add to build path" method in the IDE "Eclipse"
That is not possible, sorry. Android does not use Eclipse's "Add External JARs" regardless of whether it is manually or automatically populated. It only uses JARs in libs/ or attached Android library projects.
Eclipse doesn't provide this functionality, I believe.
You can on the other hand Select configure build path and just add the libraries.
Put the jars in the "libs" folder in your android project root folder. They will be used and exported from the build system (ant/gradle).
This link will help let you know what each folder in an android project could be used for here:
http://developer.android.com/tools/projects/index.html#ApplicationProjects
Another way would be to setup an Android Library Project which includes common dependencies of your projects, so you could add this library project as dependency in the Project->Properties->Android - Section.
Since nobody gave me an answer to my question (although they all of you did provide useful info and i think you for that) I will post the solution i found (but have not yet implemented)
I am using libgdx to create my applications so after looking and searching for a way to have eclipse automatically include libs when generating an android project, i realized that the answer was obvious (but not easy to do)...
i downloaded the libgdx source from github, learned how to use command line git and ant, and followed their instructions to get the source running. i now have the setup ui project working out of source. and am beginning my day by trying to get the libs i use included in the setup...
i know its possible and will edit this post with information on if/how i managed to accomplish this lofty goal

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.

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.

How to add a library to an android project?

I have downloaded a messaging framework library from eneter.net. In eclipse I have imported the library to the /libs folder. I right clicked on the library and then added it to the build path. I know that the code is right, but when I compile it and try to run it, it says that that it can't find the classes. Odd thing is when I open up the apk in a zipping program, the library wasn't added to the apk at compile time. Does anyone know why this would be?
Try right-clicking your project in Eclipse's project explorer, then hit Properties. Go to the tab labeled Java Build Path and hit Add Library... (or if it is a jar file, Add JARs... if you already added the Jar to your project and Add External JARs... if it is not yet within the project). From there, browse for the library you wish to add and add it to the project. Re-compile (maybe even Project->Clean) and try running it, and see if the library gets added now.
I right clicked on the library and then added it to the build path
That is not necessary, as of the R17 version of the ADT plugin in Eclipse. Moreover, it may cause problems if you do it.
Does anyone know why this would be?
Possibly because there is a typo in your directory name, such that it is not named libs/. By adding it manually to your build path, you are compiling against it, but the build tools are not adding it to the APK.
Is the library in the form of a complete Android project? This is then used with another Android project by going to the Properties of that project, selecting the Android tab, and then finding and selecting that library in the lower window.

Categories

Resources