This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between commends Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between commends "javac hello.java" and "java hello"" Watch "Difference between commends "javac hello.java" and "java hello"" New topic
Author

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

Nick Dillan
Greenhorn

Joined: Dec 23, 2011
Posts: 6

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

Joined: May 14, 2004
Posts: 511
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

Joined: Dec 23, 2011
Posts: 6

So in essence this means that javac is there to remind us of errors en java runs the program ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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.

Mohamed Sanaulla | My Blog
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
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).



Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nick Dillan
Greenhorn

Joined: Dec 23, 2011
Posts: 6

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

i think part of what threw him off was that javac requires the .java extention whereas the java command just wants the filename


SCJP
Visit my download page
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nick Dillan
Greenhorn

Joined: Dec 23, 2011
Posts: 6

Now its getting really confusing
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Dec 23, 2011
Posts: 6

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
 
subject: Difference between commends "javac hello.java" and "java hello"
 
Similar Threads
what does this error message mean?
Running compiled code
classpath problem
Use cmd and not an IDE.
@WebMethod not found