Tuesday, 11 March 2014

How to Compile & Run Java Program (.java File) using Command Prompt

There are total FOUR steps in execution of Java Program. These are as follows:-

1. Download jdk:-

 First you have to download jdk i.e. Java Development Kit. You can download this By using link.
Select your Operating System i.e OS type. Then start downloading. 


2. Installing jdk:-

 Install jdk by using the default setting.

3. Write Java Program:-  

Write Java Program which you want to execute. You can use any text editor to write Java Program. Then save that program with .java extension at your desired place. (Eg. Suppose you write java program to print Hello. And save that program with any File_name.java where File_name is name of class in java in which your main( ) method exist.) You must follow all rules while naming .java file. Because file name can't start with digit, it should not be keyword, it mus start with Letter, We can use _,etc.
In the below example I've save my Sample.java file at D:\nik.
Save .java File


4. Compile & Run .java File:-

To compile Sample.java we have to start command prompt in windows(terminal in ubantu).Then go to the path where we save program i.e. D:\nik in my case.
Then set path for java environment variable. For this Perform following steps
i. Go to C:\
ii.Go to Program Files
iii. Go to Java
 iv. Go to your jdk version (in my case it is jdk1.7.0_03)
v. Go to bin
Then copy path i.e.  C:\Program Files\Java\jdk1.7.0_03\bin
 then use following command in command prompt:
D:\nik>set path="C:\Program Files\Java\jdk1.7.0_03\bin";
then compile program by using javac File_name.java; 
D:\nik>javac Simple.java
If you compile successfully then .class file is created at D:\nik folder. 
Then execute that program by using java File_name; 
D:\nik>java Simple;
You can see OutPut. Please see following screen shot of my command prompt.
 
Screen Shot of Command Prompt



 Please give feedback. Ask doubts. Thank You.

2 comments:

  1. can you print
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1
    In java . If yes then how..

    ReplyDelete
    Replies
    1. class Number
      {
      public static void main(String args[])
      {
      for(int i=5;i>0;i--)
      {
      for(int j=1;j<=i;j++)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      }
      }


      //You can change value at i=no; where no is limit of your series

      Delete