Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

using -classpath with javac

807597May 18 2005 — edited May 23 2005
Hello again. I've made some progress since my last post ("package ultra basics") but I still have a few questions unresolved.
To reiterate I have code as follows located in the following directory on my Linux system;
/home/adunkin/myJava/world
package world;
class HelloWorld
{
    public static void main(String args[])
    {
    System.out.println("Hello (cruel) World");
    }
}
1. To achieve my goal and compile this from anywhere, I have determined that I must use the following command. However why do I have to enter the path twice here? The java command does not require this path repetition.
$ javac -classpath /home/adunkin/myJava/world /home/adunkin/myJava/world/HelloWorld.java

2. However, the following 5 versions also work and this has me baffled. It seems that for the first of these path entries required on the command line, I can enter any fragment of the path to the source code file immediately after the -classpath option. Why do I not have to enter the full path after the -classpath option?

$ javac -classpath /home/adunkin /home/adunkin/myJava/world/HelloWorld.java
$ javac -classpath /myJava /home/adunkin/myJava/world/HelloWorld.java
$ javac -classpath /home /home/adunkin/myJava/world/HelloWorld.java
$ javac -classpath / /home/adunkin/myJava/world/HelloWorld.java
$ javac -classpath /adunkin /home/adunkin/myJava/world/HelloWorld.java

3. However, leave a fragment of the path out before entering the path to the source code file and it does not work. Why not?
$ javac -classpath /home/adunkin/myJava/world/HelloWorld.java
javac: no source files
Usage: javac <options> <source files>

4. This also does not work but I thought it should as it mimics the way the java command is used. Why not?
$ javac -classpath /home/adunkin/myJava/world HelloWorld.java
error: cannot read: HelloWorld.java
1 error

5. The fact that 4. does not work has me confused as the following works;
$ java -classpath /home/adunkin/myJava world.HelloWorld
Hello (cruel) World

It seems that javac commands must be in the form;
$ javac -classpath <any fragment of the path to the source code file> <full path to the source code file>
Whereas java commands take the form;
$ java -classpath <full path to the parent directory of the package containing the class file> packageName.className
Are these two usage statements correct?

Any wise words to help me clear up these problems would be greatly appreciated.
I have studied;
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/java.html
but could not solve my problems using these documents.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 20 2005
Added on May 18 2005
20 comments
1,814 views