Setting the classpath in java using Eclipse IDE - java

I have an Eclipse project with an external folder containing a lot of JAR files (legacy libraries) . instead of adding all the jars in Eclipse ("add external Jar"), I would prefer to add a ref to this external folder. In the "configure build bath", I'm adding a "External Class Folder". If I'm doing that instead of adding the Jar, my program is not compiling anymore. Am I missing something on the way I should use this option ?
Thanks

Try this:
Project -> Properties -> Java Build Path -> Add Class Folder.
If it doesnt work, please be specific in what way your compilation fails, specifically post the error messages Eclipse returns, and i will know what to do about it.

You can create new User library,
On
"Configure Build Paths" page -> Add Library -> User Library (on list) -> User Libraries Button (rigth side of page)
and create your library and (add Jars buttons) include your specific Jars.
I hope this can help you.

Just had the same issue, for those having the same one it may be that you put the library on the modulepath rather than the classpath while adding it to your project

Related

Where to put Java libraries?

I'd like to know how I can add Java libraries to an Eclipse project on a development machine so that they can be added to an Eclipse project without causing errors when someone who has the library in a different location. For example, one developer might add an external JAR in C:\Java, but another might have the same JAR somewhere else. (Where's C:\Java on Mac OS?) I thought I might set the CLASSPATH environment variable, but I can't figure how to add an external JAR from the CLASSPATH environment variable. I'd like to do this so that it works with any workspace. Is this possible?
This is specifically for use with Anypoint Studio, but I think the same problem would exist with any Eclipse-based IDE.
In general, it's recommended to either embed JARs directly into the project, usually in a /lib folder of the project, as described here; or to use a tool like Gradle or Maven to manage dependencies, both of which have nice plugins to support their use in Eclipse.
Another alternative would be to use a Classpath Variable to refer to the JAR(s), which abstracts the physical location so that it can be set on a per-workspace basis.
I think the best way to add library to eclipse project is creating a directory - lib in your project directory. Then add the whole lib to you eclipse class path. You can follow these step to add a lib to class path -
Right click on project and select properties
Select Java Build Path
click Add Library and create User Library
Now add External Jars to this library create at step 3.
By this a .classpath file is crated in you project directory and the CLASSPATH problem will be resolved
I guess the best way to do that would be using Maven, or a similar build system that can construct your Classpath base on dependencies.
You can add the dependencies to your pom and having the jars in your local maven repository in the machine.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Because in any other approach you will need to maintain everything manually, and when having different OS the path will change.
Eclipse is just the IDE that will help to write code and assemble the project (JAR, WAR, ... ). You can add your external jars from wherever you want, and when you want to export your project (with eclipse) you may choose to package the required libraries into the jar.
However, I recommend always to use maven (or something like ) to avoid this kind of problems.
Part1:(import .jar file as library in Eclipse)
You make a new project to Eclipse(name:Project1)
When you open it you see JRE System Library[java version something]
1.right click on JRE System Library
2.Go->Build Path->Configure Build Path
3.You can see (Up right Corner the button[add jars or add external jars]
*Here i advise you to choose the first(add jars) but..
*First copy(or move) the (name).jar inside the project((example):Project 1)
*Now you can add it with the button(add jars).
*In this way when you finish your project the (name).jar will be
imported inside the project(If you export it as a .jar from Eclipse)
..Now you can call any method of (name).jar just(import it into the class
you want to use and call it)

Eclipse: updates to referenced library .jar not picked up

I have 2 Java projects in my Eclipse workspace, as follows:
One is a library of code that automatically creates a .jar file whenever it builds.
The other is an application that uses classes in that library, and references the .jar file created by the library project (by right-clicking the app -> Build Path -> Configure Build Path -> Libraries -> Add JARs...)
However, whenever I modify classes in my library project, although I can see that the library .jar gets updated, the app project doesn't pick up the changes unless I remove the library .jar from the app's referenced libraries and re-add it
Am I fundamentally doing something wrong? Is there perhaps a better way to connect an app to a shared library of code? How can I make sure my app will always refer to the latest library code?
Instead of adding it as a JAR dependency you could add it as a project dependency. Right click your project and go to properties. Click on 'Java Build Path' and select the 'Projects' tab. Click 'Add..." and add your library project.
With the latest Eclipse (I'm sure it also worked the same with the ones before), doing a refresh and Clean on your library project then on your application will update the libraries referenced.

Including a Jar in Android App

I want to include a jar into my Android package.
Unfortunately, I can't get it to work. I followed several explanations, such as this one, but I still get NoClassDefFoundErrors - at runtime, building, compiling, installing the project worked without errors.
Most answers seem to be outdated. How do I solve this issue using the current Eclipse, ADT and Android versions? Adding them to the Java Build Path like in plain Java projects didn't help.
All help appreciated.
What I have tried
Putting them into a folder and including into the project [Screenshot]
Put the jar in a folder entitled 'libs' (should be in the root of your project. Then add it to the java build path.
If that doesn't work try this:
Your Project -> right click -> Import -> File System -> yourjar.jar
Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.ja
Add your jars to libs folder. Automatically add's those JARs to your compile-time build path. More importantly, it will put the contents of the JARs into your APK file, so they will be part of your run-time build path.
You two things and the error will be fixed
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 3rd tab from top i.e "Libraries" -->Click Add Jar--> Select your Jar
Right click on Project-->Select Properties-->Go to Java Build Path--> Select 4th tab from top i.e "Order and Export" --> Select(chekbox) your Jar.
For more help check this link

Getting NoClassDefFoundError when using common.lang.StringUtils in android java code?

I am getting this error:-NoClassDefFoundError wherever the code is using StringUtils class or methods .I have correctly added the jar files to the code and is not showing any kind of error in my code(Using Eclipse Indigo).
Is there any kind of point I am missing out. I have used this jar file in other projects but they worked properly.
Please help If any one is getting similar kind of problem?Thanks
Yes, this because of your .jar file didn't import properly. Follow below steps -
Place your .jar file in your project's libs folder .
Import it into your project. And, GoTo project -> properties -> Java build path -> order tab.
Check, whether your .jar file checked and placed in order of 1st. This is the main thing.
Hope these steps helps you. Have a look at below image -
Note that the build classpath is different from the runtime classpath. You must add the required JARs to both.
It's one of the things I don't like in Eclipse...
EDIT: To add JARs to the runtime classpath: in Eclipse, double click the plugin.xml file associated with your project. On the Runtime tab there is a Classpath category. From there, you can add JARs to the runtime classpath.
Here is a screenshot to illustrate this:
Are the jar files in a lib folder or a libs folder?
Newer SDKs require the jar files to be in a libs folder.
I do not know how you execute your code, but it clearly means that the jar is not properly added to your classpath, especially if your code compiles in Eclipse.
Add it to your classpath -or re-check it is well added- when executing and it should work.
Right click on project, Select Build Path -> Configure Build Path and Select Libraries tab. Then with add Jars, add the jar in question.(if you are using eclipse)

Adding 3rd party jars to WEB-INF/lib automatically using Eclipse/Tomcat

I have a dynamic-web project set up on Eclipse and I'm using Tomcat 7 as my web server. It doesn't seem to be automatically putting 3rd party JARs I add to my library on my build path into the WEB-INF/lib folder. Is there a way I can do this automatically? Every time I search for an answer to this, I find something like this.
So how do I do that automatically? Is there a way to configure my build path to do this?
Here are the instructions for Helios. For earlier releases, see SeanA's answer.
Project Properties -> Deployment Assembly
Add -> Java Build Path Entries
You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
Select the ones you want and hit Finish.
You can use "Ant" to copy files to WEB-INF/lib when you deploy files. Using Ant you will be able to copy only those files which have changed.
http://ant.apache.org/manual/Tasks/copy.html
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.user/gettingStarted/qs-81_basics.htm
I haven't tried this personally, but what I believe it does is what I think you are looking for:
Right-click project -> Properties
Go to Java EE Module Dependencies
Check the boxes for the JARs that you want to export with your Web Application
Let me know if this automatically puts them in your exported WAR. I always just add mine to WEB-INF/lib :)
Instructios for Eclipse Indigo (version 3.7.2)
Project Properties -> Deployment Assembly
Add -> Archives From File System -> Next -> Add
(The file dialog of your system opens up.)
Navigate to the archive you want to include in your WEB-INF/lib, select it and hit OK.
Hit Finish.
Hit OK.

Categories