Saturday, September 13, 2008

First Java step: set classpath

From website: http://mindprod.com/jgloss/classpath.html

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:

  1. Put every class in a package. Don’t use the default package.

  2. 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.
  3. Configure your SET CLASSPATH= in the environment to clear it out.
  4. 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.

  1. To compile a HelloWorld.java app in the default package in C:\MyDir, use cd \MyDir
    javac.exe -classpath . HelloWorld.java
  2. To run a HelloWorld.class app, in the default package in C:\MyDir, use cd \MyDir
    java.exe -classpath . HelloWorld
  3. To compile a HelloWorld.java app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
    cd \
    javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
    rem alternatively
    e:
    cd E:\com\mindprod\mypackage
    javac.exe -classpath .;C:\ HelloWorld.java
  4. To run a HelloWorld.class app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
    cd \
    rem not you do not do cd \com\mindprod\mypackage!!
    java.exe -classpath . com.mindprod.mypackage.HelloWorld
  5. To compile a HelloWorld.class app in C:\com\mindprod\mypackage, in into a jar called helloworld.jar in package com.mindprod.mypackage, use
  6. 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
    helloworld.jar
    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.

No comments: