Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import external library by relative path in eclipse?

Tags:

eclipse

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.

like image 587
ablimit Avatar asked Feb 12 '11 19:02

ablimit


People also ask

How do I view external libraries in Eclipse?

Right-click this project folder, and select "Build Path", and then select "Configure Build Path..." Select "Java Build Path" on the left, and then the "Libraries" tab. Click the "Classpath" line, and then click the "Add External JARS..." button. Locate and select the "breadboards.


3 Answers

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.

like image 189
FrVaBe Avatar answered Nov 04 '22 18:11

FrVaBe


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.

like image 26
aljo Avatar answered Nov 04 '22 18:11

aljo


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
like image 4
glaucomardano Avatar answered Nov 04 '22 19:11

glaucomardano