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

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

问题描述

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



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



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



到目前为止,唯一适用于我的是实际更改Eclipse项目的Java构建路径。我将选择我需要的JAR并点击添加外部JAR。大多数时候,当我这样做,Eclipse会认出我的JAR并阅读其中的类。但是,在这样做时,我遇到了一些奇怪的随机错误(主要是与依赖关系有关,我认为)。由于某种原因,我只是觉得这不是正确的做法,而且我错过了一些重要的信息。何时应该手动在Eclipse中添加外部JAR,何时应该做不同的事情? Eclipse的Java构建路径似乎不了解我的CLASSPATH环境变量中的文件夹?



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



谢谢。

解决方案

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



javac java ,如果从命令提示符调用,应该/可能会遵循这个路径,但是不再认为这是很好的做法。事实证明,每个应用程序都需要自己的东西,所以一个全球性的CLASSPATH并没有真正做到这一点。现代的做法是简单地指定类路径,其中 -cp 选项在命令行上 javac java



独立的Web应用程序服务器也将设置自己的类路径。在命令行或GUI中,WebAppServers通常由脚本( .BAT .sh )启动,设置使用 -cp 的类路径。 Tomcat有一个名为 common common / lib 的目录,期望看到服务器应该可用的库,所有程序都在其下运行。但是您通常不需要/想要混淆这一点,因为应用程序的习惯是在 WEB-INF / lib 中提供自己的图书馆收藏。



所以对于一个Web应用程序,你可以将你的varous jar放入 lib 目录下, WEB-INF假设Eclipse为您预先构建了一个这样的目录结构。



您需要的所有lib都需要被Eclipse知道。在项目资源管理器中,我可以一次选择它们,右键单击并选择构建路径|添加到构建路径。这比手动修复Eclipse的项目构建路径更容易。


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.

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?

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?

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?

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.

Thank you.

解决方案

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.

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.

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.

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.

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-INF \LIB:使用什么,何时以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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