java: importing, class path, and packages

16,926

Solution 1

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

Solution 2

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

Share:
16,926
bhh1988
Author by

bhh1988

Updated on June 04, 2022

Comments

  • bhh1988
    bhh1988 almost 2 years

    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.