JDK, JRE, JVM, and Your First Java Program

JDK vs. JRE

  • JDK stands for Java Development Kit, which contains javac.exe which compiles the source code source files (.java files) into a .class file. If a source file has more than one class, each class is compiled into a separate .class file. 

  • JRE stands for Java Runtime Environment and contains the JVM (the interpreter java.exe) which reads the byte code and translates it into the native language of the host machine on the fly. 

 

Source - Compile - Execute flow

  • A java program is written in a .java file, which is called as source file.

  • javac.exe (part of JDK) which compiles the source code source files (.java files) into a .class file.

  • JVM java.exe (part of JDK and JRE) reads the byte code and translates it into the native language of the host machine on the fly, and execute.

 

First Java Program (Hello.java)

Let us now write our first java program, compile it into a .class file and then execute it using java.exe. 

You may use use notepad, notepad++ or any other plain text editor, for creating the source file.

A class can have variables (properties) and methods (funcitons).

Write a simple class called Hello with a single method called main, in a file Hello.java:

public class Hello {

  public static void main(String[] args)

  {

     System.out.println("Hello");

  }

}

Save the file Hello.java. 

Classes and methods are explained in other tutorials. The scope of this tutorial is only to execute and see the output to verify environment. 

 

Compiling the program

To compile source code file (Hello.java) from command line, go to command prompt and go to the folder where your .java file is there (e.g. using 'CD path' where path is the path to your folder.)

javac Hello.java

  • A file called Hello.class will be created.

 

Executing the program

You can execute the class file (Hello.class) from command line as:

java Hello

  • Don't mention .class.

  • Rather than writing the code in command line, you can use an IDE like Eclipse for easy writing and execution. But during the initial learning days, I would suggest you use command line for a better understanding of the basics.

 

Reference and Notes

Main Method in Java

Main method is a static method whose name is already known to Java. You executed the class without specifying any method name, but only the class name. Java will look for a method with name as main and above syntax (from example). The main method is the entry point to a desktop based core Java application. 

You can read more @ http://javajee.com/main-method-in-java.

 

Some useful DOS commands for Windows OS

To go to a path (e.g. C:\javaprograms)

CD C:\javaprograms

 

To see files in a folder

DIR

 

Rename a file (e.g. from asdfg.txt to qwert.txt)

rename asdfg.txt qwert.txt

Comments

nicely explained
 

Was it useful?
shams.tabrez.9's picture

facing some problem while i am trying to compile my program.whenever i write   " javac "   a messege appears informing javac is not recognised.
what to do ???

Was it useful?

This is because javac is not in your PATH. The PATH environment variable is something that the operating system uses to find executable files like javac. The PATH variable contains directories of executables seperated by semi colon (windows) or colon(linux). If you type "javac" in command prompt, you should have the "bin" directory of your jdk into the PATH.

In windows, you can see the value of PATH variable by typing:
path
 
To add your javac path, go to the jdk installation bin folder(make sure javac.exe is there). If your path to jdk bin folder (where javac is there) is 'C:\Program Files\Java\jdk1.6.0_13\bin', then you can add it to path in windows as:
 
set PATH=C:\Program Files\Java\jdk1.6.0_13\bin;%PATH%
 
Now verify again by executing path commaind or by executing javac commond.
 
The above step needs to be done every time you open command prompt. An alternative in windows (Windows 7) is to right click "My Computer" and select "Properties" and go to "Advanced System Properties" -> "Environment Variables". Now under System variables, edit path variable and add your path followed by a semicolon to the beginning leaving the already existing paths as is:
C:\Program Files\Java\jdk1.6.0_13\bin;<already-existing-paths>.
You need to restart command prompt to effect the above. It should be similar in other versions of windows also, just right click "My Computer" and reach Environment Variables and rest is same.
 
In Linux you can export the path as:
 
export path
 
Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!