How to add a library to an android project? - java

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.

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.

IntelliJ IDEA not recognizing JavaFX library

I am attempting to create a JavaFX project using IntelliJ IDEA. The instructions for doing so appear here: Create a new JavaFX project. This issue is, I am encountering a popup that does not appear in the official instructions, and it is preventing me from using the library.
This is the step I am having trouble with:
However, when I select the lib folder, I see this popup before I am allowed to proceed and pick a module:
No matter which option I pick, the classes from that library are still not recognized in the program. What do I do?
Edit: The lib folder is not empty, so that can't be the cause of the issue.
When you go to the lib folder select all .jar files and src.zip file then press ok. You have to manually add all files in the lib folder.

Zip Referenced Libraries into Eclipse project

I have no experience with package structure. I added two external libraries (from Joda) to my eclipse project. The code makes use of the libraries with no problems. However, when I export the Archived File and open it on another computer, the libraries are no where to be found. Much sadness.The error looks like a little red exclamation point next to my project in the package explorer. Checkboxes next to the libraries have been checked/applied.
Right click on your project
Navigate to build paths -> configure build path
In order and export tab check whether the library is in the list or not
If it is not selected, tick the Checkbox and click apply
Someone told me that I need to export it as a executable jar.

Adding External jar copy to Project

I created a project but I am using an external library for it. enter link description here
I added the library to my Beans as follows:
Right click on folder "Libraries"
Add Library
Create...
Gave it a name "gson 2.7"
Picked the class, source and javadoc files.
I went on and continued programming and then I added my code to Github. Then when I cloned it my Library was missing.
How can I fix this ? How can I make it dynamic so I dont have to worry about this anymore. In other words I need to add the library to my code and not just the reference.
Thanks a lot for the help
Just upload a copy of your library to github.
I have not used netbeans, but from what I understand it adds the jar somewhere outside of your project directory, and it simply adds the location of the jar to your classpath. When you upload and then clone the project from github, the jars are not uploaded/cloned, and the project structure metadata for your project is also lost from netbeans.
My suggestion is to learn and start using a build tool like maven. It takes up the responsibility of adding your dependency libraries everytime, given that you have an internet connection. It also adds the libs to the target directory of your project folder, so you probably will need the internet connection only once. The libs will be uploaded and cloned back from the github repo everytime.
IDEs like eclipse have inbuilt support for maven, so you don't even have to worry about executing the mvn commands. Here's a tutorial that will get you intrigued - with the IDE that you prefer.
https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html
Thanks a lot for all the replies and comments.
Technically there are two approaches to my problem. For this articular example I chose to copy the external jar files to my project.
1. Copy jar files to your project.
Go to project properties
Under Libraries folder make sure to set up a libraries folder by following the steps.
Add Library at the bottom ( you can create or import; since I already had gson in Netbeans I just imported it.)
Press ok to confirm and you are done!
2. Use Maven
Go to plugings and make sure you have all Maven plugins installed, if not do so.
Create an new project and choose maven/java application
Follow the prompts
At this point you can start coding (in my case I just imported my class files from the other project)
Right click in "Dependencies folder and then "Add Dependency"
Under query type the repository you are looking for; in my case "com.google.code.gson : gson : 2.7"
Click add and you are done!
Thanks a lot to everyone for the help :)

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

Categories

Resources