简体   繁体   English

java:导入,类路径和包

[英]java: importing, class path, and packages

I have a file which imports org.w3c.dom.Document. 我有一个导入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. 我使用locate命令尝试找到org.w3c.dom,但我什么都没得到。 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. 在我看来,正确的地方是CLASSPATH环境变量,因为我的搜索结果似乎暗示了这一点。 Is this correct? 它是否正确? In any case, I don't know how to find out what my CLASSPATH variable is. 无论如何,我不知道如何找出我的CLASSPATH变量。 It doesn't seem to be an environment variable that my shell knows about. 它似乎不是我的shell知道的环境变量。

The JVM finds classes using classpath settings where alll paths to required packages are set. JVM使用类路径设置查找类,其中设置了所需包的所有路径。 The classpath could be set with a number of ways. 可以通过多种方式设置类路径。 The first mentioned by you is CLASSPATH environment variable. 你提到的第一个是CLASSPATH环境变量。 It is optional and can be unset. 它是可选的,可以取消设置。 The second way is an explicit option "-cp" for "java" executable. 第二种方法是“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添加到类路径中,因此您不需要自己搜索和添加标准包(特别是您在问题中提到的那个)。

That would be part of the core libraries ( rt.jar ), so it'd be wherever you installed the java JRE; 那将是核心库( rt.jar )的一部分,所以它就是你安装java JRE的地方; specifically under $JAVA_HOME/jre/lib 特别是在$JAVA_HOME/jre/lib

You can look inside the .jar files using the jar command. 您可以使用jar命令查看.jar文件。 To see the class you mention, you can do: 要查看您提到的课程,您可以:

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. 请注意,JVM会自动搜索此位置 - 它不需要也不包含在CLASS_PATH环境变量中。 (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. JVM <Where_you_installed_jdk>/jre/lib包括<Where_you_installed_jdk>/jre/lib<Where_you_installed_jdk>/jre/lib/ext 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. 您必须通过-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

try compiling messconvener.java like this from its own directory 尝试从它自己的目录编译这样的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 - 为用户文件提供类路径,可以在其中找到用户定义的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM