Choose Your Language

Tuesday 18 June 2013

Running a Java Program from Command Prompt (Using Temporary path & Permanent Path)


Create a  folder C:\myjava. 
Open the Notepad and write below text 

    public class MyFirstJavaProgram
    {
      public static void main(String[] args)
      {
        System.out.println("my first java program");
      }
    }

 Save your file as MyFirstJavaProgram.java in C:\myjava. 
Note: Make sure your file name is MyFirstJavaProgram.java,
(not MyFirstJavaProgram.java.txt), first choose "Save as file type:" All files, then type in the file name MyFirstJavaProgram.java.

Run Command Prompt (start--->run dialog box will appear--->type cmd--->press enter)
   Go to myjava folder by typing
    C:\> cd C:\myjava
    This makes C:\myjava the current directory.
    C:\myjava >

To compile a nd run java program you need to set path. Two ways we can set path one is temporary path and another one is permanent path.
please refer this blog (it says how to set java path)..click here

Using Temporary path
    C:\myjava > set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
    This tells the system where to find JDK programs.
    C:\myjava > javac MyFirstJavaProgram.java
    This runs javac.exe, the compiler.  You should see nothing but the next system prompt...

    C:\myjava >
    javac has created the MyFirstJavaProgram.class file.  You should see MyFirstJavaProgram.java and   MyFirstJavaProgram.class among the files.

    C:\myjava > java MyFirstJavaProgram

    output:
    my first java program

Using Permanent Path
 C:\myjava > javac MyFirstJavaProgram.java
    This runs javac.exe, the compiler.  You should see nothing but the next system prompt...

    C:\myjava >
    javac has created the MyFirstJavaProgram.class file.  You should see MyFirstJavaProgram.java and MyFirstJavaProgram.class among the files.

    C:\myjava > java MyFirstJavaProgram

    output:
    my first java program

No comments:

Post a Comment