java:导入、类路径和包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7857256/
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
java: importing, class path, and packages
提问by bhh1988
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.
我有一个导入 org.w3c.dom.Document 的文件。编译和运行很好,但我不明白它是如何知道在哪里可以找到这个包的,我只是好奇它是如何工作的。我使用 locate 命令尝试查找 org.w3c.dom 但我什么也没得到。这些包在哪里?在我看来,CLASSPATH 环境变量是正确的查看位置,因为我的搜索结果似乎暗示了这一点。这个对吗?无论如何,我不知道如何找出我的 CLASSPATH 变量是什么。它似乎不是我的 shell 知道的环境变量。
采纳答案by Brian Roach
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
那将是核心库 ( rt.jar
) 的一部分,因此它会出现在您安装 java JRE 的任何地方;具体在$JAVA_HOME/jre/lib
You can look inside the .jar
files using the jar
command. To see the class you mention, you can do:
您可以.jar
使用该jar
命令查看文件内部。要查看您提到的课程,您可以执行以下操作:
jar tvf rt.jar
This lists all the classes in that jar.
这列出了该 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)
请注意,此位置由 JVM 自动搜索 - 它不需要也不包含在 CLASS_PATH 环境变量中。(你可以添加它,但它只是多余的)
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 the CLASS_PATH
environment variable.
JVM 包括<Where_you_installed_jdk>/jre/lib
和<Where_you_installed_jdk>/jre/lib/ext
默认情况下。其他任何内容都必须由您通过-cp
选项直接传递给 java或将其添加到CLASS_PATH
环境变量中来显式添加。
The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
相关文档可以在以下位置找到:http: //download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html
回答by xappymah
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.
JVM 使用类路径设置查找类,其中设置了所需包的所有路径。可以通过多种方式设置类路径。您提到的第一个是 CLASSPATH 环境变量。它是可选的,可以取消设置。第二种方法是“java”可执行文件的显式选项“-cp”。
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).
此外,默认情况下,一些 JRE 运行时 jar 被隐式添加到类路径中,因此您无需自己搜索和添加标准包(尤其是您在问题中提到的那个)。
回答by saikumarm
try compiling messconvener.java like this from its own directory
尝试从它自己的目录中编译这样的 messconvener.java
javac -d ..\..\. -cp ..\..\. messconvener.java
javac -d ..\..\. -cp ..\..\. messconvener.java
-d - creates directory structure for your package
-d - 为你的包创建目录结构
-cp - provides class path for user file, where it can find user defined classes
-cp - 提供用户文件的类路径,它可以在其中找到用户定义的类