CLASSPATH、Java Buld Path (eclipse) 和 WEB-INF\LIB:使用什么、何时使用以及为什么使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1821571/
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-12 22:58:18  来源:igfitidea点击:

CLASSPATH, Java Buld Path (eclipse), and WEB-INF\LIB : what to use, when, and why?

javaeclipsejakarta-eejarclasspath

提问by sangfroid

I recently switched to J2EE from .NET, and am confused about where to put JAR files. I know that the CLASSPATH, WEB-INF, and Eclipse's Java Web Path are all places where JARs can be stored, but I'm confused about which folder to use, when, and why.

我最近从 .NET 切换到 J2EE,并且对放置 JAR 文件的位置感到困惑。我知道 CLASSPATH、WEB-INF 和 Eclipse 的 Java Web Path 都是可以存储 JAR 的地方,但我对使用哪个文件夹、何时使用以及为什么使用感到困惑。

First off, we have the CLASSPATH. I usually set this by going into "Environment Variables" inside "My Computer." I know that this is the default place where the Java compiler looks for JAR files. When I add a folder or a JAR to my CLASSPATH environment variable, why is it ignored by Eclipse, the Java compiler, and the web server?

首先,我们有 CLASSPATH。我通常通过进入“我的电脑”中的“环境变量”来设置它。我知道这是 Java 编译器查找 JAR 文件的默认位置。当我将文件夹或 JAR 添加到我的 CLASSPATH 环境变量时,为什么 Eclipse、Java 编译器和 Web 服务器会忽略它?

Also, I know that WEB-INF\LIB is a place where you can put JAR files that your web app is going to use. However, I've put JARs in WEB-INF\LIB only to have them be ignored. In what situations should I put JARs into WEB-INF\LIB folder? How do I get Eclipse or the web server to notice them?

另外,我知道 WEB-INF\LIB 是一个可以放置 Web 应用程序将要使用的 JAR 文件的地方。但是,我将 JAR 放在 WEB-INF\LIB 中只是为了忽略它们。在什么情况下我应该将 JAR 放入 WEB-INF\LIB 文件夹?如何让 Eclipse 或 Web 服务器注意到它们?

So far, the only thing that works for me is to actually change the Java Build Path for an Eclipse project. I'll select the JARs I need and hit "Add External JARs." Most of the time when I do this, Eclipse will recognize my JARs and read the classes therein. However, I've run into a bunch of weird random errors while doing this (mostly having to do with dependencies, I think). For some reason, I just get the feeling that this isn't the right way to do things, and that I'm missing some vital piece of information. When should I be manually Adding External JARs inside Eclipse, and when should I be doing things differently? How come Eclipse's Java Build Path doesn't seem to know about the folders in my CLASSPATH environment variable?

到目前为止,唯一对我有用的是实际更改 Eclipse 项目的 Java 构建路径。我将选择我需要的 JAR 并点击“添加外部 JAR”。大多数情况下,当我这样做时,Eclipse 会识别我的 JAR 并读取其中的类。但是,我在执行此操作时遇到了一堆奇怪的随机错误(我认为主要与依赖项有关)。出于某种原因,我只是觉得这不是正确的做事方式,而且我错过了一些重要的信息。我什么时候应该在 Eclipse 中手动添加外部 JAR,什么时候我应该做不同的事情?为什么 Eclipse 的 Java Build Path 似乎不知道我的 CLASSPATH 环境变量中的文件夹?

Really, I would just like to gain a better understanding of the CLASSPATH, Eclipse's Java Build Path, and the WEB-INF/LIB folder -- the purposes they serve, the relationships between them, and where I should be putting my JARs in various situations. I would appreciate any advice you could give me, or any articles that you could recommend.

真的,我只是想更好地了解 CLASSPATH、Eclipse 的 Java 构建路径和 WEB-INF/LIB 文件夹——它们服务的目的、它们之间的关系以及我应该将 JAR 放在不同的位置情况。我很感激你能给我的任何建议,或者你可以推荐的任何文章。

Thank you.

谢谢你。

采纳答案by Carl Smotricz

The CLASSPATHyou set in your environment affects only standalone Java applications, i.e. ones you run from a command prompt or an icon. As you've noticed, Eclipse ignores this. It sets up its own per-project classpaths.

CLASSPATH您的环境设置只影响独立的Java应用程序,即你从命令提示符下运行或图标的。正如您所注意到的,Eclipse 忽略了这一点。它设置了自己的每个项目的类路径。

javacand java, if called from the command prompt, should/may honor this path, but it's no longer considered great practice to do this. It's turned out that every app needs its own set of stuff, so a global CLASSPATH isn't really doing any of them any good. Modern practice is to simply specify the classpath with the -cpoption on the command line for javacor java.

javac并且java,如果从命令提示符调用,应该/可能遵循这条路径,但不再认为这样做是很好的做法。事实证明,每个应用程序都需要自己的一套东西,因此全局 CLASSPATH 对它们中的任何一个都没有任何好处。现代做法是简单地使用-cp命令行上的选项指定类路径javacjava

A standalone Web Application server will also set up its own classpath. From the command line or GUI, WebAppServers are usually started by a script (.BATor .sh) that sets up a classpath using -cp. Tomcat has a directory called commonor common/libwhere it expects to see libraries that should be available the the server and all programs running under it. But you will generally not need/want to mess with this, as it's customaries for applications to provide their own library collectons in WEB-INF/lib.

独立的 Web 应用程序服务器还将设置自己的类路径。从命令行或 GUI 中,WebAppServers 通常由脚本(.BAT.sh)启动,该脚本使用-cp. Tomcat 有一个名为commonor的目录common/lib,它希望在该目录中看到服务器和在该目录下运行的所有程序可用的库。但是您通常不需要/不想弄乱这个,因为应用程序习惯于在WEB-INF/lib.

So for a Web app, you'd put your varous jars into the libdirectory, under WEB-INF, assuming Eclipse pre-builds such a directory structure for you.

因此,对于 Web 应用程序,假设 Eclipse 为您预先构建了这样的目录结构,您可以将各种 jars 放入lib目录中,在 下WEB-INF

All the libs you need also need to be made known to Eclipse. In the Project Explorer, I select the whole slew of them at once, right-click and select Build Path | add to build path. That's easier than messing with Eclipse's project build path manually.

您需要的所有库也需要让 Eclipse 知道。在项目资源管理器中,我一次选择所有这些,右键单击并选择Build Path | add to build path. 这比手动弄乱 Eclipse 的项目构建路径更容易。

回答by Powerlord

If you're dealing with web applications, /WEB-INF/lib is the portable place to put JARs. This is where web serversservlet containers expect to find an application's jar files.

如果您正在处理 Web 应用程序,/WEB-INF/lib 是放置 JAR 的可移植位置。这是Web 服务器servlet 容器期望找到应用程序的 jar 文件的地方。

回答by Diego Dias

Eclipse requires you to specify the path to your libraries, jar files (on Properties -> Java Build Path -> Libraries tab). This can be found on the .classpath project file.

Eclipse 要求您指定库、jar 文件的路径(在 Properties -> Java Build Path -> Libraries 选项卡上)。这可以在 .classpath 项目文件中找到。

Usually you have the JRE libs on its path (which would be on your classpath too), so adding the libs to the classpath and updating eclipse build path would work.

通常你的路径上有 JRE 库(它也会在你的类路径上),所以将这些库添加到类路径并更新 eclipse 构建路径会起作用。

The WEB-INF directory should be the place that contains necessary information for your web application.

WEB-INF 目录应该是包含 Web 应用程序必要信息的地方。

回答by Laazik

I'm not an Eclipse expert, but I think that your problem can be answered like this:

我不是 Eclipse 专家,但我认为您的问题可以这样回答:

1) CLASSPATH is an environment variable that is read in when you launch java programs and is used by classloader to figure out where the classes are placed.

1) CLASSPATH 是一个环境变量,它在您启动 java 程序时被读入,并被类加载器用来确定类的放置位置。

I would modify the CLASSPATH variable only in the case when you are launching an java program from a script, as this allows you to conveniently launch a program and make sure that the classes are found. This would not be the case for you, as you are developing the web application.

我只会在您从脚本启动 java 程序的情况下修改 CLASSPATH 变量,因为这允许您方便地启动程序并确保找到类。对您而言,情况并非如此,因为您正在开发 Web 应用程序。

2) WEB-INF/lib is the directory under which the web application container's classloader (like tomcat or glassfish) looks into if your web application needs to resolve a class. So you put there the classes that are used in your web application.

2) WEB-INF/lib 是Web 应用程序容器的类加载器(如tomcat 或glassfish)在您的Web 应用程序需要解析类时查看的目录。因此,您将 Web 应用程序中使用的类放在那里。

Some IDE's do include the libraries/.jar files that you are using in a project automatically to the package.

某些 IDE 确实将您在项目中使用的库/.jar 文件自动包含到包中。

3) Eclipse library/classpath resolving during the development time. I would assume, but apologies for assuming, as one really shouldn't do this ;), that you can define a library (add external .jar files to projects) and autocomplete/all the other interesting features should start working with this, as you basically make those classes visible for the IDE with that activity. I also would assume that you can then mark those libraries to be automatically added to the web projects etc., by the IDE.

3) Eclipse 库/类路径在开发期间解析。我会假设,但很抱歉假设,因为人们真的不应该这样做;),您可以定义一个库(将外部 .jar 文件添加到项目中)和自动完成/所有其他有趣的功能应该开始使用它,因为您基本上可以通过该活动使这些类对 IDE 可见。我还假设您可以将这些库标记为由 IDE 自动添加到 Web 项目等中。

In general a good reading about how classes are found during execution is here(it's a sun's official documentation). Also a good place to read about this is the ClassLoader class documentation.

一般来说,关于如何在执行过程中找到类的好读物在这里(这是一个 sun 的官方文档)。ClassLoader 类文档也是阅读此内容的好地方。

回答by Thorbj?rn Ravn Andersen

Java has a long history and experience has shown that some ideas were good and some were bad.

Java 有着悠久的历史,经验表明,有些想法是好的,有些是坏的。

The CLASSPATH environment variable was the initial way to tell the Java machine where to locate classes from your program, and works reasonably well for command line programs. It was rapidly found that this should not be a global thing (as that tend to mess things up in the long run) but a per-program thing. This could be done by creating a wrapper script/BAT-file which sets the variable and runs the Java machine.

CLASSPATH 环境变量是告诉 Java 机器从您的程序中找到类的位置的最初方式,并且对于命令行程序运行得相当好。人们很快发现这不应该是一个全局性的事情(因为从长远来看这往往会使事情变得一团糟),而是每个程序的事情。这可以通过创建一个设置变量并运行 Java 机器的包装脚本/BAT 文件来完成。

All was well, then people wanted to write web server stuff in Java. The Servlet API was created where a web application is a stand-alone unit - this resulted in that the CLASSPATH for eachweb application isthe unpacked files under WEB-INF/classes plus the jar-files under WEB-INF/lib. And only that. This means the global CLASSPATH variable is ignored. This has been found to be a VERYgood thing, so the concept has migrated elsewhere.

一切都很好,然后人们想用 Java 编写 Web 服务器的东西。创建了 Servlet API,其中 Web 应用程序是一个独立单元 - 这导致每个Web 应用程序的 CLASSPATHWEB-INF/classes 下的解压缩文件加上 WEB-INF/lib 下的 jar 文件。而仅此而已。这意味着忽略全局 CLASSPATH 变量。这被发现是一件非常好的事情,所以这个概念已经迁移到其他地方。

For instance a "executable jar" (which Eclipse calls a "runnable jar") which is invoked with "java -jar foobar.jar" contains the complete classpath INSIDE the Jar in a special manifest file. Java Web Start which is used to start java programs from a web server explicily lists the full classpath in the configuration file on the server.

例如,使用“java -jar foobar.jar”调用的“可执行 jar”(Eclipse 称为“可运行 jar”)在特殊清单文件中包含 Jar 内部的完整类路径。Java Web Start 用于从 Web 服务器启动 Java 程序,它在服务器上的配置文件中明确列出了完整的类路径。

But, to get you started. If you want to write a Java web application:

但是,让你开始。如果要编写 Java Web 应用程序:

  1. Get the Eclipse Java EEversion.
  2. Create a new Dynamic Web Project e.g. named foobar.
  3. Drag and drop (or copy/paste) the jar files you need in foobar/WebContent/WEB-INF/lib
  4. Create a new file named foobar/WebContent/index.jsp. In the blank file type <h1>Hello World <%= new java.util.Date() %></h1>
  5. Right click in editor for index.jsp, choose Run -> Run on Server, and choose the Basic -> J2EE preview at localhost server, and Finish.
  1. 获取 Eclipse Java EE版本。
  2. 创建一个新的动态 Web 项目,例如名为 foobar。
  3. 在 foobar/WebContent/WEB-INF/lib 中拖放(或复制/粘贴)您需要的 jar 文件
  4. 创建一个名为 foobar/WebContent/index.jsp 的新文件。在空白文件类型中<h1>Hello World <%= new java.util.Date() %></h1>
  5. 在 index.jsp 的编辑器中右键单击,选择 Run -> Run on Server,然后选择 Basic -> J2EE preview at localhost server,然后选择 Finish。

A browser window will now open, either in a browser or inside Eclipse which will render your JSP-page. You can change the JSP-page, save it with Ctrl-S and reload the browser window to see the changes.

现在将在浏览器中或 Eclipse 中打开一个浏览器窗口,它将呈现您的 JSP 页面。您可以更改 JSP 页面,使用 Ctrl-S 保存它并重新加载浏览器窗口以查看更改。

回答by BalusC

Also, I know that WEB-INF\LIB is a place where you can put JAR files that your web app is going to use. However, I've put JARs in WEB-INF\LIB only to have them be ignored. In what situations should I put JARs into WEB-INF\LIB folder? How do I get Eclipse or the web server to notice them?

另外,我知道 WEB-INF\LIB 是一个可以放置 Web 应用程序将要使用的 JAR 文件的地方。但是,我将 JAR 放在 WEB-INF\LIB 中只是为了忽略它们。在什么情况下我应该将 JAR 放入 WEB-INF\LIB 文件夹?如何让 Eclipse 或 Web 服务器注意到它们?

The realproblem you have here is likely that you didn't got Eclipse for Java EEdevelopersand/or that you just created a generic Java Projectinstead of a Dynamic Web Projectand built up the necessary folder structure yourself.

您在这里遇到的真正问题很可能是您没有为 Java EE开发人员提供Eclipse和/或您只是创建了一个通用Java 项目而不是动态 Web 项目并自己构建了必要的文件夹结构。

If you create a Dynamic Web Projectin Eclipse for Java EE developers, then Eclipse will automagically add any libraries in WEB-INF/libto the build path. The build pathis roughly said just the classpath which is been used in both compiletime and runtime. With other words: just drop the 3rd party JAR's in there, really nothing more needs to be done.

如果您在Eclipse 中为 Java EE 开发人员创建一个动态 Web 项目,那么 Eclipse 会自动将任何库添加到构建路径中。在构建路径大致刚才说这是在两种编译时和运行时被使用的类路径。换句话说:只需将第 3 方 JAR 放入其中,就无需再执行任何操作了。WEB-INF/lib

Note that Java is case sensitive, thus it should be really called WEB-INF/lib, not WEB-INF/LIB. But anyway, if you create a Dynamic Web Project, then Eclipse will just automagically generate the correct folder/file structure for you.

请注意,Java 区分大小写,因此应该真正调用它WEB-INF/lib,而不是WEB-INF/LIB. 但无论如何,如果您创建一个动态 Web 项目,那么 Eclipse 将自动为您生成正确的文件夹/文件结构。

As said by others, ignore the %CLASSPATH%environment variable. It is onlyused by javac.exe/java.exe and even then onlywhen you do notspecify any of the -cp, -classpathor -jararguments. In real world this environment variable is seldom used, it is just some convenience for starters (and unfortunately also the most confusing one, they should never have invented it).

正如其他人所说,忽略%CLASSPATH%环境变量。它由 javac.exe/java.exe 使用,并且在您指定任何-cp,-classpath或 -jar参数时使用。在现实世界中,这个环境变量很少使用,它只是为初学者提供了一些便利(不幸的是也是最令人困惑的一个,他们不应该发明它)。

回答by Michael

Taken together the comments helped me as well. I had added all the jena .jars to the build path from eclipse but that wasn't sufficient. Following suggestion to "add to WEB-INF/lib" it seemed intuitive to drag from libraries folder to WEB-INF (from within eclipse), but that didn't work. Nor did copying the .jars to WEB-INF. I eventually drag-and-dropped from the windows desktop to the WEB-INF lib folder in Eclipse, and that fixed the problem. It would be nice if any .jars added to the build path were automatically copied to WEB-INF lib by Eclipse. In case it matters, this was eclipse EE IDE, Indigo release, on windows 7.

综合起来,这些评论也对我有帮助。我已经将所有 jena .jars 添加到 eclipse 的构建路径中,但这还不够。按照“添加到 WEB-INF/lib”的建议,从库文件夹拖动到 WEB-INF(从 eclipse 中)似乎很直观,但这不起作用。也没有将 .jars 复制到 WEB-INF。我最终从 Windows 桌面拖放到 Eclipse 中的 WEB-INF lib 文件夹,并解决了问题。如果 Eclipse 将添加到构建路径的任何 .jar 文件自动复制到 WEB-INF 库,那就太好了。以防万一,这是 Windows 7 上的 Eclipse EE IDE,Indigo 版本。