How to import external library by relative path in eclipse?

Eclipse

Eclipse Problem Overview


I imported external libraries using absolute path. But I have two work environments, switching between Linux and Windows. Projects are downloaded from SVN. So I was wondering whether I can import these libraries by relative path.

Eclipse Solutions


Solution 1 - Eclipse

You should declare a variable (Java Build Path -> Add Variable... -> Configure Variable ... -> New) to set the changing path on each system (e.g. FOO_BAR_HOME).

Than you can add the Variable to the Libraries and edit it to point to your library, like

%FOO_BAR_HOME%/lib/foobar.jar

Take a look at the existing variables for usage.

Alternative you can place the library inside the project (e.g. subfolder 'lib'). If you add the library from this location ('Add Jars...' NOT 'Add External Jars...') it will be added by relative path.

Solution 2 - Eclipse

Hey I was having this issue as well, but I can offer an alternate solution for those who want to keep the required library locally but outside the directory of the Java Project.

First, add the library you want using the usual "Add External Library/Jar" method.

If you go into the home directory of your Java Project, you should see a .classpath file. Edit this with any text editor you wish, and look for the line that references the library you added. Then, say your library is called lib.jar and you have it one level outside of your project.

Lets say the line says:

<classpathentry kind="lib" path="C:/Users/Public/workspace/lib.jar"/>

Rather than moving lib.jar to your Project's directory, you can just change this line to say:

<classpathentry kind="lib" path="./../lib.jar"/>

and your Java Build path will be updated to look for it in the directory one level above the current project.

Also, to my knowledge if you're running a Mac or any Unix based OS, then the .classpath file will be hidden. In order to access it, you can try opening it from Terminal, by navigating to the directory that your Java Project uses (the actual project folder itself, not the workspace), then either vi or vim ".classpath". Keep in mind that using ls or even ls -a might not work, but trust that it will be there.

Once you've changed it to the specified location, save the file and when you go back into eclipse, the reference should still be working. Keep in mind that the "./" at the beginning is necessary. Just having "../" will not work, or at least it didn't for me.

Solution 3 - Eclipse

I did it very simple. I had a lib with an absolute path in my classpath:

/home/glauco/workspace/ltClubs/lib/swingx-core-1.6.2.jar

So i just removed the absolute path from it and it works. Now it's relative xD:

lib/swingx-core-1.6.2.jar

Solution 4 - Eclipse

This should be a comment on the previous answer, but the strange reputation system from this site forces me to post a new answer instead... (no comments)

You can use a relative path, but you're missing the './' in it.

Instead of

lib/swingx-core-1.6.2.jar

you should use

./lib/swingx-core-1.6.2.jar

Solution 5 - Eclipse

Add the folder containing the dependencies into the JAVA project. Select and right-click the dependencies, "Add to Build Path" pops up as a context menu. This adds the dependencies using relative path instead of the absolute path.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionablimitView Question on Stackoverflow
Solution 1 - EclipseFrVaBeView Answer on Stackoverflow
Solution 2 - EclipsealjoView Answer on Stackoverflow
Solution 3 - EclipseglaucomardanoView Answer on Stackoverflow
Solution 4 - EclipseluispabloView Answer on Stackoverflow
Solution 5 - EclipsegunaprsdView Answer on Stackoverflow