java:导入、类路径和包
我有一个导入 org.w3c.dom.Document 的文件。编译和运行很好,但我不明白它如何知道在哪里可以找到这个包,我只是好奇它是如何工作的。我使用locate命令尝试查找org.w3c.dom,但一无所获。这些包位于哪里?在我看来,正确的查找位置是 CLASSPATH 环境变量,因为我的搜索结果似乎表明了这一点。这是正确的吗?无论如何,我不知道如何找出我的 CLASSPATH 变量是什么。它似乎不是我的 shell 知道的环境变量。
I have a file which imports org.w3c.dom.Document. Compiling and running is fine, but I don't understand how it knows where to find this package and I'm just curious how it works. I used the locate command to try and find org.w3c.dom but I get nothing. Where are these packages located? It seems to me that the right place to look would the CLASSPATH environment variable since my search results seem to be suggesting that. Is this correct? In any case, I don't know how to find out what my CLASSPATH variable is. It doesn't seem to be an environment variable that my shell knows about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将是核心库 (
rt.jar
) 的一部分,因此它会位于您安装 java JRE 的任何位置;特别是在$JAVA_HOME/jre/lib
下,您可以使用
jar
命令查看.jar
文件内部。要查看您提到的类,您可以执行以下操作:这列出了该 jar 中的所有类。
请注意,该位置由 JVM 自动搜索 - 它不需要也不包含在 CLASS_PATH 环境变量中。 (您可以添加它,但这只是多余的)
为清楚起见进行编辑:
JVM 包括
/jre/lib
和和
。其他任何内容都必须由您通过 -cp 选项直接传递给 java 或将其添加到 CLASS_PATH 环境变量中来显式添加。/jre/lib
默认情况下为 /jre/lib/ext相关文档可以在以下位置找到: http://download.oracle .com/javase/6/docs/technotes/tools/findingclasses.html
That would be part of the core libraries (
rt.jar
), so it'd be wherever you installed the java JRE; specifically under$JAVA_HOME/jre/lib
You can look inside the
.jar
files using thejar
command. To see the class you mention, you can do:This lists all the classes in that jar.
Note that this location is automatically searched by the JVM - it's not needed nor included in the CLASS_PATH environment variable. (You could add it, but it would simply be redundant)
Edit for clarity:
The JVM includes
<Where_you_installed_jdk>/jre/lib
and<Where_you_installed_jdk>/jre/lib/ext
by default. Anything else has to be explicitly added by you via either passing it to java directly via the-cp
option or adding it to theCLASS_PATH
environment variable.The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
JVM 使用类路径设置查找类,其中设置了所需包的所有路径。可以通过多种方式设置类路径。您首先提到的是 CLASSPATH 环境变量。它是可选的并且可以取消设置。第二种方法是“java”可执行文件的显式选项“-cp”。
另外,一些 JRE 运行时 jar 默认情况下会隐式添加到类路径中,因此您不需要自己搜索和添加标准包(特别是您在问题中提到的那个)。
The JVM finds classes using classpath settings where alll paths to required packages are set. The classpath could be set with a number of ways. The first mentioned by you is CLASSPATH environment variable. It is optional and can be unset. The second way is an explicit option "-cp" for "java" executable.
Also some JRE runtime jars are added to classpath by default implicitly so you don't need to search and add standard packages by yourself (particulary the one you mentioned in your question).
中像这样编译messvenner.java。
尝试从它自己的目录javac -d ..\..\ -cp ..\..\. messconvener.java
-d - 为您的包创建目录结构
-cp - 提供用户文件的类路径,在其中可以找到用户定义的类
try compiling messconvener.java like this from its own directory
javac -d ..\..\. -cp ..\..\. messconvener.java
-d - creates directory structure for your package
-cp - provides class path for user file, where it can find user defined classes