简体   繁体   中英

Add jar file to Eclipse project with relative path

I have a Java project in Eclipse. How can I add a jar file to the eclipse project with a relative path? That means the jar file is stored in the project directory and the project should still work when its whole directory is moved.

Using external jar works, but this is not what I want!

I also know that I should use 'Add jar' and not 'Add external jar'. But 'Add external jar' shows my selectable jar file and 'Add jar' does not. Why?? When I change the absolute path in the class file to relative path the project does not work anymore.

How the hell can I make Eclipse referring a jar relatively and not absolutely. I also wonder why this is so hard to do.

How the hell can I make Eclipse referring a jar relatively and not absolutely.

If the library is only for your project, bring that jar file in a folder inside your project and then you'll be able to add it from project's preferences via "Add Jar"

If you think the library is usable across many projects, define a "User library" (in general Eclipse's "Preferences/Java/Build Path/User library" - start from Eclipse's "Windows" menu). Also there, you can note you can add more than one JAR files to what Eclipse calls "User library" - that should be a good-thing™ for extra dependencies

The "Add external library" is provided for the (should be rare) cases when the two above are impossible for some reasons.

I also wonder why this is so hard to do.

If you take the time to look what you can do with a JAR file once specified in Eclipse, you'll note that the JAR entry isn't just a file, but supports the extra information of where the source and Javadoc files are located (just click on that triangle/arrow on the left of the library once you added it). Which information, once filled in, allows the "wonders" of:

  • debugging through the library source when called from your code
  • the popup with the Javadoc entry when you hover over a method declared by the library
  • more friendly autocomplete

I feel these are worth the price of a bit of extra time setting up the library entry the way Eclipse wants them, and I can guarantee I'm not alone in my feeling.


Also, take my advice: the less time you spend frustrated by an IDE's functionality that doesn't work the way you think it should, the more time you can put into developing new code. After all, if you don't like an IDE, feel free to take others, write your own or adjust one at your choice (Eclipse being open source and all that).

Last but not least: language, my dear frustrated friend, watch it. Your questions on "how to do it" and "why" are legitimate (therefore I answered them), but the use expletives brings you closer to "matter of opinions" which is a no-no on SO.

To add relative path in your .classpath file in eclipse please do the following steps.

1) Right click on your Project -> New menu and Select submenu 'Folder' -> type your folder name asked in popped up dilog box -> and Click on Finish.

2) Now go to your eclipse workspace -> Take your all jar files from hard disk and paste it into the lib folder OR you can also copy the jar files -> right click on lib folder -> select paste menu from list and paste it.

3) After paste, right click on project in eclipse -> Click on Build Path -> Select submenu Configure Build Path -> In the popped up dialog go to libraries tab and click the Add JARs... Button -> Select lib folder and select all the jar inside the lib folder -> then finally click on Apply and Close.

Add the jar files in classpath using a relative path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM