Eclipse: Use environment variable for external jar location

10,445

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).

Then you can add the variable to the Libraries section and edit it to point to your library, like:

%FOO_BAR_HOME%/lib/foobar.jar

Take a look at the existing variables for usage.

Alternatively 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.

Share:
10,445
LalaBox
Author by

LalaBox

Updated on June 04, 2022

Comments

  • LalaBox
    LalaBox almost 2 years

    After hours of unsuccessful googling, I ask you:

    Situation:

    My Eclipse Project uses "nedded.jar" so I added it to the build path. No problem.

    • C:/dev/development/my_needed/nedded.jar

    But the location of "nedded.jar" is relative to the environment variable DEVELOPMENT ( =C:/dev/development/ ) and therefore may change. So, I need my Referenced Library path to be:

    • %DEVELOPMENT%/my_needed/nedded.jar

    I could not find the syntax to accomplish that. Ideas?

    EDIT:

    Maybe I did not make myself clear enough: This Project is developed by MSVisual Studio(C++) and Eclipse(Java). Both are started from the Console. A prior executed script sets 3 major variables: DEVELOPMENT, RUNTIME, SOURCES to certain, changing paths.

    If I then start Eclipse from this shell, the path to my external libs shall be defined by %DEVELOPMENT%\my_needed\nedded.jar.

    I have found a half way solution (somewhere): added a new folder --> advanced --> link to alternate location (linked folder).

    this adds you an entry in your .project, which I also get, when I checkout my project:

    <linkedResources>
       <link>
       <name>lib/RXTXcomm.jar</name>
       <type>1</type>
         <locationURI>PARENT-4-PROJECT_LOC/Development/rxtx/RXTXcomm.jar</locationURI>
       </link>
    </linkedResources>
    

    Nice so far, but it still does not depend on %DEVELOPMENT%. What I need is: %DEVELOPMENT%/rxtx/RXTXcomm.jar

    Hope this is now clearer.

  • kc2001
    kc2001 about 10 years
    +1 for helping me out. Here's something to add (for Eclipse 3.6.2, at least)... After adding your FOO_BAR_HOME VARIABLE, you can edit the Java Build Path for your project. From the Libraries tab, click "Add Variable", select your FOO_BAR_HOME variable, and click the "Extend" button. Then, navigate to your jar file and select it. Your .classpath file will end up with a path relative to your FOO_BAR_HOME variable.