• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between commends "javac hello.java" and "java hello"

 
Nick Dillan
Greenhorn
Posts: 6
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple question. In a lot of tutorials (including the book Head First: Java) they instruct to type "javac hello.java" for example to run the java file.

When I do this it does nothing. When I try "Java Hello" it runs my java file. I make my code using notepad++ and use cmd to navigate to the correct folder.
 
amit punekar
Ranch Hand
Posts: 544
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick,
"javac" helps you to compile your code i.e. file with a .java extension.
"java" runs your compiled code that you wrote to meet your requirement.
Hope this helps.

Regards,
amit
 
Nick Dillan
Greenhorn
Posts: 6
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in essence this means that javac is there to remind us of errors en java runs the program ?
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Vc wrote:So in essence this means that javac is there to remind us of errors en java runs the program ?

javac converts your code into a format (byte code) that can be executed by the JVM. In the process it reports the errors and if there are any errors then the class file (which is executed by the jvm) is not created.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Vc wrote:...for example to run the java file.


You never run a java file. You only compile it and get a class file.
You always run class file and not java file.

You can javac a 'java' file and get 'class' file.
You can java (on command prompt) a 'class' file and it is then you actually run it.

A java file is also known as source code file.
A class file is also known as 'byte code'.

You can understand contents of java file.
A class file is understood by JVM which I believe interprets and runs it. (That is why they say java has both a compiler and an interpreter).


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this page from Oracle's Java Tutorials.

It works as follows:

1. You create a Java source file, which is just a text file with the extension ".java", which contains the source code of your program.
2. You use the Java compiler, javac, to convert it to a ".class" file.
3. You use the java command to run the code in the .class file.

 
Nick Dillan
Greenhorn
Posts: 6
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah great info guys thank you very much. It is a little bit of a desert when starting out with programming !
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think part of what threw him off was that javac requires the .java extention whereas the java command just wants the filename
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i think part of what threw him off was that javac requires the .java extention whereas the java command just wants the filename

I though the java command wants the CLASS name, not file name.
 
Nick Dillan
Greenhorn
Posts: 6
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now its getting really confusing
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Dillan wrote:Now its getting really confusing


But in fact it's really very simple
Four simple steps:
1 write your java program and save it in the your_filename.java (with .java extension)
2 call javac your_filename.java to compile this programm (javac requires the .java extension)
3. If compilation in step 2 failed, go to step 1 and correct errors in your program
4. call java your_filename (without .java extension) to run this programm
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javac command (the compiler) expects the filename of the source file as an argument. The filename has a ".java" extension, so that's what you have to type when you invoke the compiler. For example: javac MyProgram.java

The java command (that runs the compiled program) expects the class name (not a filename!) of the class to run. If your class is named "MyProgram", the command to use is: java MyProgram (without ".class", because that's not part of the name of the class). Ofcourse it will try to load a file named "MyProgram.class" when you call it - but the point to understand is that you're not specifying the filename directly, but the name of the class.
 
Nick Dillan
Greenhorn
Posts: 6
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ow ok, that really does make sense now. Thanks all for the input.

Ireneusz and Jespers post made it cristal clear
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic