The CLASSPATH is an environment variable that tells the Java compiler javac.exe where to look for class files to import or java.exe where to find class files to interpret.
In contrast, the PATH is an environment variable that tells the command processor the where to look for executable files, e.g. *.exe, *.com and *.bat files. The Classpath is one of the most confusing things in Java. Unfortunately, you must master it to even compile HelloWorld.
CLASSPATH And File Naming RecipesHere are my simplified rules for using CLASSPATH and naming the files on the javac.exe and java.exe command line:
Put every class in a package. Don’t use the default package.
Use the latest JDK. It will be the one everyone you ask help from is familiar with. Two dangling prepositions in one sentence. Churchill would be proud.
- Configure your SET CLASSPATH= in the environment to clear it out.
- You don’t need to explicitly include rt.jar:
rt.jar system class files :
in C:\Program Files\java\jre1.6.0_07\lib\rt.jar in the JRE 1.6.0 on your local hard disk.
in J:\Program Files\java\jdk1.6.0_07\jre\lib\rt.jar in the in the JDK 1.6.0 on your local hard disk.
in C:\Program Files\java\jre1.5.0_16\lib\rt.jar in the older JRE 1.5.0 on your local hard disk.
in J:\Program Files\java\jdk1.5.0_16\jre\lib\rt.jar in the in the older JDK 1.5.0 on your local hard disk.
unless you are using Jikes, where you need to add it to the JIKESPATH, but not the CLASSPATH.
In all that follows, everything is strictly case sensitive.
- To compile a HelloWorld.java app in the default package in C:\MyDir, use cd \MyDir
javac.exe -classpath . HelloWorld.java
- To run a HelloWorld.class app, in the default package in C:\MyDir, use cd \MyDir
java.exe -classpath . HelloWorld
- To compile a HelloWorld.java app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
javac.exe -classpath .;C:\ HelloWorld.java
- To run a HelloWorld.class app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
rem not you do not do cd \com\mindprod\mypackage!!
java.exe -classpath . com.mindprod.mypackage.HelloWorld
- To compile a HelloWorld.class app in C:\com\mindprod\mypackage, in into a jar called helloworld.jar in package com.mindprod.mypackage, use
- To run a HelloWorld.class app in C:\com\mindprod\mypackage, in a jar called helloworld.jar in package com.mindprod.mypackage, use CD \dirwherejaris
java.exe -classpath helloworld.jar com.mindprod.mypackage.HelloWorld
rem or if your Main-Class manifest points to HelloWorld.class.
java.exe -jar helloworld.jar
rem or if your *.jar extension is set to invoke java.exe
If for any reason the examples shown do not work with your version of java.exe, try replacing the \ in com\mindprod\mypackage\HelloWorld with / mypackage/HelloWorld.If you stare long enough at those examples, you may understand the logic behind them, and then you can create variants. If you can’t, just slavishly copy the closest matching example.