JDK, JRE, JVM, and Your First Java Program

JDK vs. JRE

  • JDK stands for Java Development Kit, which contains the compiler executable javac.exe. The java.exe executable compiles the source code (.java files) into an intermediate form called as byte code ( .class files). 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 executable). The java.exe executable reads the byte code (.class file), translates it into the native language of the host machine and execute it. 

 

Source - Compile - Execute flow

  • A java program is written in a .java file, which is called as source code 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)

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

Let us 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. 

Note: 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 present (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.

References 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
 
You voted 'DOWN'.
Was it useful?

As  I am Very New to Programming so I am Confused when coming to this programming point especially on 7th step, could you pls explain.. Thank  you

 

Was it useful?

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)