Java 外部罐子放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/325524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:12:41  来源:igfitidea点击:

Where to put the external jars?

javaeclipsejareclipse-3.4ganymede

提问by Burkhard

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

我使用 Eclipse (3.4) 并且我的类编译时没有警告或错误。我的项目使用外部 jar 文件。

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

我需要把这个外部 jar 文件放在哪里,以便java.lang.NoClassDefFoundError在从另一个项目(不在 Eclipse 中)使用这个类时不会得到一个?

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

我可以将 jar 解压缩到项目文件夹中,但这感觉不对。

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

编辑:这个问题不是关于在 Eclipse 中导入 jars,而是在 Eclipse 之外使用它们。

采纳答案by James Camfield

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.

如果您想将 JAR 文件包含到您的 Eclipse 项目中,您通常会在项目文件夹中创建一个“lib”文件夹,并将该文件放在那里。然后您需要告诉 eclipse 将它包含在您的类路径中,以便您的代码将在 eclipse 中编译和运行。

要做到这一点:
- 进入项目的属性
- 在左侧栏中选择“Java 构建路径”
- 选择窗口中间部分的“库”选项卡
- 单击添加 JAR 按钮 - 这将为您提供您在 eclipse 中的项目列表 - 展开您的项目并进入 lib 文件夹 - 您的 jar 将在那里。
- 选择 JAR,单击确定,然后再次退出属性窗口。

Your code will now compile and run.

您的代码现在将编译并运行。

回答by Jon Skeet

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.)

你把它放在哪里并不重要,但你需要配置你的其他非 Eclipse 项目,将外部 jar 放在它的类路径中——或者使用扩展目录机制,如果你必须的话。(这更容易,但可以说使其明确更好。)

回答by guerda

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.

我把它放到一个新文件夹中。此文件夹必须包含在构建路径中。它是在 Eclipse 中还是在外部都没有关系。Eclipse 有一个特定于项目的构建路径,并将此路径传递给 javac。如果要在 Eclipse 之外执行 javac,则必须手动传递构建路径。

回答by VonC

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

您只需要-classpath在一个不包含在 eclipse 工作区中的文件夹中使用一个选项来引用它,即它根本不依赖于 eclipse。

Warning, you cannoy execute your other project with java -jarif you reference your external jar with -cpoption (see this question)

警告,java -jar如果您使用-cp选项引用外部 jar,则无法执行其他项目(请参阅此问题

回答by Simon

put it in your jre/lib/extfolder

把它放在你的jre/lib/ext文件夹里

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.

您可以通过查看 Windows 上的 JAVA_HOME 环境变量来找到您的 jre 文件夹。

回答by javamonkey79

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.

我知道这不完全是您要问的,但是您是否考虑过使用 Maven?在这种情况下,诸如程序集之类的一些插件可能会有所帮助。

回答by JeeBee

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

正如 James Camfield 所写,在 Eclipse 中有一个 jar。

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.

然后,当您构建(Ant、Maven、手动)进行分发时,请确保 jar 文件包含在您的应用程序 jar 或 war 或 ear 或任何文件中,并且任何启动脚本都使用 -classpath 命令行将其包含在类路径中Java 的选项,如 VonC 所写。

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.

不要担心将 jars 放在 java 扩展文件夹中,所有这些只会让您在将代码发送给第三方使用时忘记它,因为他们不会在最后设置它.

回答by Robin

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.

创建一个共享的 lib 目录并将这个 jar 和任何其他常见的 jar 文件放在那里。如果您使用的是 Maven,那么您已经以本地存储库的形式拥有它。

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.

在 Eclipse 中,为此目录创建一个类路径变量,然后使用它来引用项目中的 jar 文件。如果您有多个满足这些条件的项目,那将让生活变得更加轻松。如果您从本地驱动器转到网络驱动器,它还可以轻松更改根目录的位置。

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

在外部应用程序中,您还将在应用程序类路径上的共享位置包含此 jar 文件。

回答by user3132194

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

西蒙的答案似乎是最好的,但现在有点过时了。用谷歌搜索了这个 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.

从 Java 6 开始,扩展 JAR 文件也可以放置在独立于任何特定 JRE 的位置,以便系统上安装的所有 JRE 可以共享扩展。

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

它说对于 Windows,你应该把你的扩展放在 %SystemRoot%\Sun\Java\lib\ext 。