外部罐子放在哪里?
我使用 Eclipse (3.4),我的类编译时没有警告或错误。 我的项目使用外部 jar 文件。
我需要将该外部 jar 文件放在哪里,以便在从另一个项目(不在 Eclipse 中)使用此类时不会出现 java.lang.NoClassDefFoundError 错误?
我可以将 jar 解压到项目文件夹中,但这感觉不对。
编辑:这个问题不是关于在 Eclipse 中导入 jar,而是在 Eclipse 之外使用它们。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您想要将 JAR 文件包含到 Eclipse 项目中,通常会在项目文件夹中创建一个“lib”文件夹,并将该文件放入其中。 然后,您需要告诉 Eclipse 将其包含在您的类路径中,以便您的代码将在 Eclipse 中编译并运行。
为此:
- 进入项目的属性
- 在左侧栏中选择“Java 构建路径”
- 选择窗口中央部分的“库”选项卡
- 单击“添加 JAR”按钮 - 这将为您提供 Eclipse 中的项目列表 - 展开您的项目并进入 lib 文件夹 - 您的 jar 将在那里。
- 选择 JAR,单击“确定”,然后在属性窗口中再次单击“确定”。
您的代码现在将编译并运行。
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.
把它放在你的
jre/lib/ext
文件夹中,关于类路径的所有内容都是正确的,但这是它存在的一个一致且合理的地方。
您可以通过查看 Windows 上的 JAVA_HOME 环境变量来找到您的 jre 文件夹。
put it in your
jre/lib/ext
foldereverything 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.
将其放在哪里并不重要,但是您需要配置其他非 Eclipse 项目以将外部 jar 放在其类路径中 - 或者使用扩展目录机制(如果必须的话)。 (这更容易,但使其明确可以说更好。)
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.)
西蒙的答案似乎是最好的,但现在有点过时了。 已通过 Google 搜索此 Oracle 文档 已安装的扩展 。
它表示,对于 Windows,您应该将扩展放在此处 %SystemRoot%\Sun\Java\lib\ext 。
Simon's answer seems to be the best but a bit outdated now. Have googled this Oracle doc Installed extensions .
It says that for Windows you should place your extensions here %SystemRoot%\Sun\Java\lib\ext .
您只需要使用
-classpath
选项在不包含在 Eclipse 工作空间中的文件夹中引用它,即根本不依赖于 Eclipse。警告,如果您使用
-cp
选项引用外部 jar (查看此问题)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)我将其放入一个新文件夹中。 该文件夹必须包含在构建路径中。
无论是在 Eclipse 中还是在外部,都没有关系。 Eclipse 有一个项目特定的构建路径,并将该路径传递给 javac。
如果要在 Eclipse 外部执行 javac,则必须手动传递构建路径。
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.
我知道这并不完全是您要问的,但是您考虑过使用 Maven 吗? 某些插件(例如 assembly)在此类情况下可能会有所帮助。
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.
正如 James Camfield 所写,在 Eclipse 中放置一个 jar。
然后,当您构建(Ant、Maven、手动)进行分发时,请确保 jar 文件包含在您的应用程序 jar 或 war 或ear 或任何文件中,并且任何启动脚本都使用 -classpath 命令行将其包含在类路径中java 的选项,正如 VonC 所写。
不用担心将 jar 粘贴到 java 扩展文件夹中,这一切只会让您在将代码发送给第三方使用时忘记它,因为他们不会在最后设置它。
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.
创建一个共享 lib 目录并放置此 jar 以及您可能拥有的任何其他常见 jar 文件。 如果您使用 Maven,那么您已经以本地存储库的形式拥有它。
在 Eclipse 中,为此目录创建一个类路径变量,然后使用它来引用项目中的 jar 文件。 如果您有多个满足这些条件的项目,那么生活就会变得更加轻松。 如果您从本地驱动器转到网络驱动器,还可以轻松更改根目录的位置。
在外部应用程序中,您还将从应用程序类路径上的共享位置包含此 jar 文件。
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.