Differences between javac -classpath and java -classpath commands

Hi all,

I am a beginner in Java. Have been trying the whole day to find out whats the diff between javac -classpath and java -classpath? I know javac compiles and java runs, but why both we have to specify the classpath?

I wrote a simple Test.java program to print something. But when i include a package statement in my Test.java file, the program throws a NoClassDefFoundError: Test (wrong name: ...)? why?

I have been really confused with classpaths and packages, the whole day was spent reading docs and the forum, hope somebody can tell me here? i just need to find out why is there such an error, then i will use that to know the concepts, thanks!

[678 byte] By [overtime_ea] at [2007-10-2 5:01:21]
# 1

If you want to understand classpath use (and you really should, otherwise you will continue to have inexplicable errors), I recommend that you go to this page:

http://java.sun.com/j2se/1.4.2/docs/tooldocs/tools.html

and read the following documents. (ncluded in the java and javac documents are explanations of how each of them use the classpath - it's too complicated to discuss here.)

Setting the Classpath

How Classes are Found

Basic Tools (javac)

Basic Tools (java)

Here is a minimal explanation of packages that will explain the wrong name error:

Assume that your programs are part of a package named myapp, which is specified by this first line in each source file:

package myapp;

Also assume that directory (C:\java\work\) is listed in the CLASSPATH list of directories.

Also assume that all your source files reside in this directory structure: C:\java\work\myapp\

Then a statement to compile your source file named aProgram.java is:

C:\java\work\>javac myapp\aProgram.java

And a statement to run the program is:

java myapp.aProgram

(This can be issued from any directory, as Java will search for the program, starting the search from the classpath directories.)

Explanation:

Compiling

A class is in a package if there is a package statement at the top of the class.

The source file needs to be in a subdirectory structure. The subdirectory structure must match the package statement. The top subdirectory must be in the classpath directory.

So, you generate a directory structure C:\java\work\myapp\ which is the [classpath directory + the package subdirectory structure], and place aProgram.java in it.

Then from the classpath directory (C:\java\work\) use the command: javac myapp\aProgram.java

Running

-

Compiling creates a file, aProgram.class in the myapp directory.

(The following is where people tend to get lost.)

The correct name now, as far as java is concerned, is the combination of package name and class name: myapp.aProgram (note I omit the .class) If you don't use this name, java will complain that it can't find the class.

To run a class that's NOT part of a package, you use the command: java SomeFile (assuming that SomeFile.class is in a directory that's listed in the classpath)

To run a class that IS part of a package, you use the command java myapp.aProgram (Note that this is analogous to the command for a class not in a package, you just use the fully qualified name)

ChuckBinga at 2007-7-16 1:05:11 > top of Java-index,Developer Tools,Java Compiler...
# 2
Thanks ChuckBing!!I think you answered my doubts very comprehensively! Now I fully understand why I need to do all these!
overtime_ea at 2007-7-16 1:05:11 > top of Java-index,Developer Tools,Java Compiler...