Running A Java Program From Command Prompt
In order to run a Java program from the command prompt, follow the steps below carefully:
- Create a temporary folder
C:\mywork
. Using Notepad or another text editor, create a small Java fileHelloWorld.java
with the following text:public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Learn how to create a helloworld app.
- Save your file as
HelloWorld.java
inC:\mywork
. To make sure your file name isHeloWorld.java
, (notHelloWorld.java.txt
), first choose "Save as file type:" All files, then type in the file name HelloWorld.java.
- Run Command Prompt (found under All Programs/Accessories in the Start menu).
Type
C:\> cd \mywork
C:\mywork> dir
HelloWorld.java
among the files.C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
C:\mywork> javac HelloWorld.java
javac.exe
, the compiler. You should see nothing but the next system prompt...C:\mywork> dir
HelloWorld.class
file. You should seeHelloWorld.java
andHelloWorld.class
among the files.C:\mywork> java HelloWorld
Hello, World!
java HelloWorld
command. 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 a mistake. 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 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). Do not put spaces before the appended path string.
Click OK on the path edit box and OK on the Ennvironment Variables box. The new setting will go into effect next time you run Command Prompt
0 comments:
Post a Comment