How to Make Java .jar files be automatically found in any environment

410 views Asked by At

I am trying to add an external library to my Java project. The library files are in .jar format, and every time someone tries to run the project in a different environment, they have to modify the build path so that their environment can find the .jar files.

Is there any way to make it so that the .jar files are automatically found by the build path? Maybe by specifying where in the project the .jar files are located?

Edit: Here is an example - In my linux environment, the .jar files are located in:

/home/MyUsername\SomeDirectory\workspace\java_project_name\data

In my windows environment, the .jar files are located in:

C:\MyUsername\SomeDirectory\workspace\java_project_name\data

In the example above, the project root directory is "java_project_name", and the .jar files are always in the java_project_name\data folder. Even though this is the case, different environments can't seem to detect that it is always in the same path in relation to the project's root directory.

I have looked in many places on how to do this, and some people told me it isn't possible. This doesn't sound reasonable to me, which is why I am posting this question here.

Thanks in advance for your help!

3

There are 3 answers

0
Manish Kr. Shukla On BEST ANSWER

This is a common problem for developement environments.

I myself use the following solution in netbeans [Hope so that it works in your IDE too]:

  1. Create an Ant variable [for e.g. JAR_LOCATION].
  2. Each and every JAR that you refer, must have it's location relative to JAR_LOCATION.

Now, in every development environment that you're gonna use, you can set up the same ANT variable and it'll automatically pick up the JAR.

You can see the example in the given snapshot of netbeans..enter image description here

0
MeBigFatGuy On

you can add a ClassPath: entry in your manifest file of the main jar, and use relative paths. As long as the jars are found relative to the main jar it will work.

0
liorsolomon On

This issue was one of my main motivators to start using maven. @YoK nicely quotes here the relevant reasons to this question https://stackoverflow.com/a/3589930/1493954

Here is one the relevant quotes from Benefits of using Maven

Henning

  • quick project setup, no complicated build.xml files, just a POM and go
  • all developers in a project use the same jar dependencies due to centralized POM.
  • getting a number of reports and metrics for a project "for free"
  • reduce the size of source distributions, because jars can be pulled from a central location