Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac and java

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic