Jan 14, 2014

Compile and Run Java Program in Command Window

I cannot recall the old days when we used the command window to compile and run a Java program, because we get used to use IDE like Eclipse. But it worth to make a note here:

1. Say you have write a simple program Hello.java, which will print "Hello World", for example the program is in directory called D:\temp

public class Hello{
 public static void main(String[] args){
  System.out.println("Hello World");
 }
}


2. Launch a command window, and go to folder D:\temp
javac Hello.java 
3. Here is the important part:
java -cp . Hello

Note:
-cp is the option to specify where the class path is,
. means the current folder, you can also specify a detail folder like D:\temp


If you find this blog is useful, please kindly click the ads on this page to help. Thank you very much.

No comments: