外放罐放在哪里? [重复]

时间:2023-02-05 11:40:47

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.

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

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

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

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中导入jar,而是在Eclipse之外使用它们。

9 个解决方案

#1


54  

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 Build Path” - 选择窗口中间部分的“Libraries”选项卡 - 单击Add JARs按钮 - 这将为您提供在eclipse中列出你的项目 - 扩展你的项目并进入lib文件夹 - 你的jar将在那里。 - 选择JAR,单击“确定”,然后再单击“属性”窗口中的“确定”。

Your code will now compile and run.

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

#2


19  

put it in your jre/lib/ext folder

把它放在你的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文件夹。

#3


7  

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放在它的类路径中 - 或者如果你必须的话,使用扩展目录机制。 (这更容易,但明确表示更好。)

#4


5  

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

西蒙的答案似乎是最好的,但现在有点过时了。用谷歌搜索了这个Oracle doc Installed扩展。

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中。

#5


1  

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.

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

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

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

#6


0  

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,则必须手动传递构建路径。

#7


0  

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吗?像汇编这样的插件可能会在这些情况下有所帮助。

#8


0  

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命令行将其包含在类路径中vonC编写的java选项。

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.

不要担心将jar放在java扩展文件夹中,所有这一切都会让你在将代码发送给第三方时忘记它,因为它们不会在最后设置它们。

#9


0  

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.

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

#1


54  

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 Build Path” - 选择窗口中间部分的“Libraries”选项卡 - 单击Add JARs按钮 - 这将为您提供在eclipse中列出你的项目 - 扩展你的项目并进入lib文件夹 - 你的jar将在那里。 - 选择JAR,单击“确定”,然后再单击“属性”窗口中的“确定”。

Your code will now compile and run.

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

#2


19  

put it in your jre/lib/ext folder

把它放在你的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文件夹。

#3


7  

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放在它的类路径中 - 或者如果你必须的话,使用扩展目录机制。 (这更容易,但明确表示更好。)

#4


5  

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

西蒙的答案似乎是最好的,但现在有点过时了。用谷歌搜索了这个Oracle doc Installed扩展。

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中。

#5


1  

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.

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

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

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

#6


0  

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,则必须手动传递构建路径。

#7


0  

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吗?像汇编这样的插件可能会在这些情况下有所帮助。

#8


0  

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命令行将其包含在类路径中vonC编写的java选项。

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.

不要担心将jar放在java扩展文件夹中,所有这一切都会让你在将代码发送给第三方时忘记它,因为它们不会在最后设置它们。

#9


0  

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.

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