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条回答
一纸荒年 Trace。
2楼-- · 2019-01-16 12:53

It doesn't matter too much where you put it, but you need to configure your other non-Eclipse project to put the external jars in its classpath - or use the extensions directory mechanism, if you must. (That's easier, but making it explicit is arguably better.)

查看更多
我命由我不由天
3楼-- · 2019-01-16 12:58

put it in your jre/lib/ext folder

everything said about the classpath is true, but this is a consistent and sensible place for it to live.

you can find out your jre folder by looking at the JAVA_HOME environment variable on Windows.

查看更多
来,给爷笑一个
4楼-- · 2019-01-16 13:07

Have a jar inside Eclipse, as James Camfield has written.

Then when you build (Ant, Maven, manually) for distribution, ensure the jar file is included with or within your application jar or war or ear or whatever file, and that any startup scripts include it on the classpath using the -classpath command line option for java, as VonC has written.

Don't worry about sticking the jars in the java extensions folder, all this will do is make you forget about it when it comes to sending your code to a third party to use, because they won't have it set up at their end.

查看更多
登录 后发表回答