CLASSPATH、Java Buld Path (eclipse) 和 WEB-INFLIB:使用什么、何时使用以及为什么使用? [英] CLASSPATH, Java Buld Path (eclipse), and WEB-INFLIB : what to use, when, and why?

查看:13
本文介绍了CLASSPATH、Java Buld Path (eclipse) 和 WEB-INFLIB:使用什么、何时使用以及为什么使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

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

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

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?

真的,我只是想更好地了解 CLASSPATH、Eclipse 的 Java 构建路径和 WEB-INF/LIB 文件夹——它们的用途、它们之间的关系以及我应该把我的各种情况下的 JAR.如果您能给我任何建议,或者您可以推荐任何文章,我将不胜感激.

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 仅影响独立的 Java 应用程序,即您从命令提示符或图标运行的应用程序.正如您所注意到的,Eclipse 忽略了这一点.它为每个项目设置了自己的类路径.

The CLASSPATH you 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.

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

javac and 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 -cp option on the command line for javac or java.

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

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 (.BAT or .sh) that sets up a classpath using -cp. Tomcat has a directory called common or common/lib where 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 应用程序,您将各种 jar 放入 lib 目录中的 WEB-INF 下,假设 Eclipse 为你.

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

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

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.

这篇关于CLASSPATH、Java Buld Path (eclipse) 和 WEB-INFLIB:使用什么、何时使用以及为什么使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆