Wednesday, January 28, 2015

Java: Compile and run your program in command line

A friend needed some help to debug a Java program. I have JDK installed, but no Java IDE. Instead of installing one, command line come to rescue! ;)

To compile the .java files:
javac *.java

You can use the -d option to specify where to place generated class files, e.g.
javac -d "build" *.java

To run the compiled code:
java <class_name>
e.g. java HelloWorld

You can use the -cp option to specify the class search path, e.g.
java -cp "build" HelloWorld

If you defined a package, you'll need to specify it, e.g.
java -cp "build" MyPackage.HelloWorld

No comments: