How to import external library by relative path in 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.
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
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.