aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes javac and java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "javac and java" Watch "javac and java" New topic
Author

javac and java

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
what is the difference between javac -classpath bin Bar.java and
java -classpath bin Bar

suppose i am in my main directory and in it there is bin folder that contains all class files....except the file Bar.java and its a class file is in the main dir


the javac command compiles...but why doesnt the java command?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

The classpath (list of) directory is used by the compiler and the JVM to find all the class files it uses. Interestingly, the java compiler doesn't need the Bar.class file -- because it doesn't need it to compile the Bar.java file.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
because it doesn't need it to compile the Bar.java file.


what does this mean? am i not compiling the Bar.java file here?...or do you mean while javac command was executed we didnt have any class file of Bar.java

and why does the java command give me class not found error?

is it because the jvm looks for all the classes in the bin folder but the Bar.class was actually compiled in the main directory?

i think this makes sense


Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
so it means we should use -classpath in the java command to include the directory which contains the class file of Bar.java....as it wouldnt need any other class to compile now
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

The javac command is the Java compiler. When you enter a command such as javac Bar.java, then you are compiling the source file Bar.java to a class file Bar.class. Make sure you don't confuse "compiling" and "running": javac, the Java compiler, compiles source code to class files, and java, the launcher, runs class files.

The classpath tells the compiler (javac) or the Java launcher (java) where it should look for class files - not Java source files or anything else.

Raju Champaklal wrote:so it means we should use -classpath in the java command to include the directory which contains the class file of Bar.java....as it wouldnt need any other class to compile now

No. The java command doesn't do anything with source files such as Bar.java and does not compile your code. It just runs Java programs, and you might use the -classpath switch to tell it where to look for the file Bar.class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac and java