当前位置: 动力学知识库 > 问答 > 编程问答 >

import - java: importing, class path, and packages

问题描述:

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.

网友答案:

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 the jar command. To see the class you mention, you can do:

jar tvf rt.jar 

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 the CLASS_PATH environment variable.

The relavent documentation can be found at: http://download.oracle.com/javase/6/docs/technotes/tools/findingclasses.html

网友答案:

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).

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: