Where to put the external jars? [duplicate]

2019-01-16 12:19发布

This question already has an answer here:

I use Eclipse (3.4) and my class compiles without warning or errors. My project uses an external jar file.

Where do I need to put this external jar file in order not to get a java.lang.NoClassDefFoundError when using this class from another project (not in Eclipse)?

I could just extract the jar into the project folder, but that does not feel right.

Edit: this question is not about importing jars in Eclipse, but using them outside of Eclipse.

9条回答
虎瘦雄心在
2楼-- · 2019-01-16 12:41

If you're wanting to include a JAR file to your Eclipse project, you would generally create a 'lib' folder inside the project folder, and put the file in there. You then need to tell eclipse to include it in your class path so your code will compile and run inside eclipse.

To do that:
- Go into the properties of your project
- Select 'Java Build Path' in the left hand column
- Select the 'Libraries' tab in the centre part of the window
- Click the Add JARs button - this will give you a list of your projects in eclipse - expand your project and into the lib folder - your jar will be there.
- Select the JAR, click OK, and OK again out of the properties window.

Your code will now compile and run.

查看更多
ら.Afraid
3楼-- · 2019-01-16 12:41

I know this isn't exactly what you are asking, but have you considered using Maven? Some of the plugins like assembly might be helpful in situations like these.

查看更多
SAY GOODBYE
4楼-- · 2019-01-16 12:43

Create a shared lib directory and place this jar, and any other common jars files you might have there. If you are using Maven, then you already have it in the form of the local repo.

In Eclipse, create a classpath variable for this directory and then use it to reference your jar file in your projects. If you have multiple projects that meet these conditions, it will make life much easier. It also makes it easy to change the location of the root directory if you go from a local drive to a network one.

In the external app, you will also include this jar file from its shared location on the apps classpath.

查看更多
神经病院院长
5楼-- · 2019-01-16 12:44

Simon's answer seems to be the best but a bit outdated now. Have googled this Oracle doc Installed extensions .

As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system.

It says that for Windows you should place your extensions here %SystemRoot%\Sun\Java\lib\ext .

查看更多
小情绪 Triste *
6楼-- · 2019-01-16 12:47

You just need to reference it using a -classpath option, in a folder which is not included in an eclipse workspace, i.e. which does not depend on eclipse at all.

Warning, you cannoy execute your other project with java -jar if you reference your external jar with -cp option (see this question)

查看更多
对你真心纯属浪费
7楼-- · 2019-01-16 12:52

I place it into a new folder. This folder has to be included into the build path. It does not matter if it's in Eclipse or outside. Eclipse has a project specific build path and it passes this path to the javac. If you want to execute javac outside of Eclipse, you have to pass the build path manually.

查看更多
登录 后发表回答