Monday 4 February 2013

Running a Java Program from Command Prompt

Create a temporary folder in drive C:\mywork. 

Using Notepad or any other text editor, create a Java file HelloWorld.java and wirte the code given below:

public class HelloWorld
     {
        public static void main (String[] args)
           {
                System.out.println ("Hello World Program!!");
           }
}
Save your file as HelloWorld.java in a folder C:\mywork. And make sure that your file name is HeloWorld.java,(should not HelloWorld.java.txt). 

To check it first you have to choose "Save as file type: "All files then after that type in the file name HelloWorld.java

Run Command Prompt (Which is found under All Programs/Accessories in the Start menu). And Type
C:\> cd \mywork
It is to make C:\mywork the current directory.
C:\mywork> dir
This displays the directory contents which are in mywork. Below that you should see HelloWorld.java among the list.
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
C:\Program Files\Java\jdk1.7.0_11
This line tells the system where to find JDK programs.
C:\mywork> javac HelloWorld.java
This runs javac.exe, the compiler.  You should see nothing but the next system prompt...
C:\mywork> dir
javac has created the HelloWorld.class file.  You can see HelloWorld.java and HelloWorld.class among those list.
C:\mywork> java HelloWorld

This runs the Java interpreter.  You should see the program output:
Hello, World!

If the system cannot find javac, check the set path command.  If javac runs but you get errors, should have to check your Java text properly. If the program compiles but you get an exception, check capitalization and the spelling in the file name and the class name and the java HelloWorld command. As we know Java is case-sensitive!

It is possible to make the path setting permanent but you have to be very careful because your system might crash if you make any mistake then you should have to Proceed with extreme caution!
In Windows XP, go to Control Panel, choose "System," click on the "Advanced" tab, click on the "Environment variables" button. In the lower list, "System variables," click on the Path:

Click "Edit" and at the end append ; C:\Program Files\Java\jdk1.5.0_09\bin
(or the path to the appropriate folder where the latest version of JDK is installed).  Try to not put spaces before the appended path string. 

Click OK on the path edit box and OK on the Environment Variables box.  
The new setting will now go into effect next time when you run Command Prompt.

1 comment:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2022