Articles → Java → Create Compile And Execute Your First Java Program

Create Compile And Execute Your First Java Program






Software Requirement







Prerequisite Knowledge





Installation Of JDK





Create Your First Program




  1. Create a folder on any location to store java code files. Let us say a folder ‘files’ is created on c drive.
  2. Open a notepad (or any text editor) and paste the following program
  3. class MainClass {
      public static void main(String args[]) {
        System.out.println("Hello World");
      }
    }


  4. Save the file with the name MainClass.java (you can give any file name, but the file extension should be .java) on the location ‘C:\files’.

Code Explanation




  1. A class MainClass is created.
  2. Inside the MainClass we have a method called main method (program execution starts with main function).
  3. Inside the main method we are printing a string ‘Hello World’.

Compile Your Program




  1. Open command prompt and go the location ‘C:\Program Files\Java\jdk1.7.0_07\bin’.
  2. Write following command in command prompt
  3. javac  c:\files\MainClass.java




  4. Press enter.



Execute Your Program




  1. Open command prompt (if the command prompt opened to compile the program is not closed).
  2. Go to the location ‘C:\files’.
  3. Write the following command in the command prompt
  4. java MainClass




  5. Press enter.

Output


Picture showing the output of the first java program
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Monday, September 10, 2012

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250