3

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.

1
  • my opinion is it a bad idea to use a CLASSPATH environment variable anyway. It's a better option to pass the -classpath parameter to either java or javac. Commented Oct 22, 2011 at 3:56

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

I found rt.jar using locate, because I don't have a JAVA_HOME environment variable. Is it a problem that I don't have this environment variable? What is it for and who is responsible for setting it?
@bhh1988 - I was merely using JAVA_HOME in my answer to illustrate where the file was located; I would have no idea where you installed java on your machine ;) Like CLASS_PATH, it's also not needed to run Java programs - much like how I used it in my example, it's generally used for other things to be able to find where you installed java on your machine.
CLASSPATH, not CLASS_PATH
Thanks so much for mentioning jar tvf command. For a Java newbie it was very difficult to determine how to get package names that are contained in the jar and this helped immensely.
3

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

2 Comments

i see. Are there any other jars that are being added implicitly? Where can I find a list of those jars which are added implicitly to the class path? And how do view my classpath?
Well in fact you may look at your JDK installation directory to check the runtime jars (rt.jar). But those jars are not displayed in classpath as they are the part of the Java runtime library.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.