Compiling and Running Java from Command Line

In the last lesson we wrote the first Java program – Hello CodingRaptor! . We also dissected the program word by word. Now we will see how to run the program on command line.

If you have not already done so, now would be a good time to review how to install JDKuse Netbeansuse Eclipseuse programming editors for Java. If you use IDEs or editors – which you should for real projects, you don’t need to compile and run Java programs from command line. Nevertheless, running from command line is a fundamental step in learning Java and re-inforces your respect for package structures and CLASSPATH. In this article, we use javac and java from command line. As long as you have JDK installed on your system, implying have javac and java in your PATH, it does not matter whether you are on UNIX command line or Windows command line.

Packages for Namespace Separation

Java packages are both a security feature and a namespace mechanism. Using packages we can be sure that our file will not run into conflict with a file with same name created by someone else.

As an example suppose you create a class MoneyGenerator and put it in a file called MoneyGenerator.java .  Money, being the magnet it is, may also inspire others to create class by the name MoneyGenerator. And if you happen to put a jar file containing the foreign MoneyGenerator.class , your application will silently start using this foreign class. The foreign MoneyGenerator is likely to have different methods. For example, you may have a method by the name workHard() but the foreign MoneyGenerator may have method like rob(). At the very least signatures of the method will not match and your application will fail with NoSuchMethodError.

Packages help solve this potential clash. You associate your file with a directory structure that uniquely identifies you. For example, you may write programmer.MoneyGenerator and the other dishonest one might come from politician.MoneyGenerator. JVM is now able to differentiate between the two as long as you are clear in your import statements.

In our case, since we own codingraptor.com, we use the package name as com.codingraptor. This translates into directory structure of com as the outermost directory and codingraptor as its subdirectory. Our class file HelloCodingRaptor.java lies in the codingraptor directory.

This inversion of name helps in organizing your code. Many if not most organizations have domain extensions such as com, net, org (Remember this language decision was made long before domain extensions such as guru, club and me became commonplace). Since you may end up with code from multiple organizations, and most of them are likely to be dotcoms,  you will have one com directory and each organization representend as a subdirectory. This way the code from various organization is cleanly segregated.

Complying with Package Structure

 Since our package name is com.codingraptor, we need to follow these steps

  1. Create a directory called com
  2. Create a subdirectory codingraptor inside com
  3. Place the HelloCodingRaptor.java file inside codingraptor directory

Remember, on UNIX like systems such as Solaris and Linux the case of directory name matters. The convention is to use all small case for directory names and CamelCase for name of Java classes and files.

Compiling Java Files

For compiling Java file follow these steps

  1. Open command line of your operating system – cmd.exe or power shell or gnome-terminal or rxvt or xterm depending on your OS.
  2. Navigate to the directory where you created com directory. You should be able to see com if you do a ls or dir.
  3. run the following command
    12345# on Windowsjavac com\codingraptor\HelloCodingRaptor.java # on UNIX like systemsjavac com/codingraptor/HelloCodingRaptor.java
  4. This will create a class file by the name HelloCodingRaptor.class in the codingraptor directory
  5. You can specify CLASSPATH to Java compiler using javac -classpath option.

Running Class Files from Command Line

Running java is quite similar to running javac. Follow these steps

  1. Open command line of your operating system – cmd.exe or power shell or gnome-terminal or rxvt or xterm depending on your OS.
  2. Navigate to the directory where you created com directory. You should be able to see com if you do a ls or dir.
  3. run the following command
    1234# on Windows and UNIX like# also demonstrates architecture-# neutrality of Javajava com.codingraptor.HelloCodingRaptor
  4. On running the above command, you should be able to see the desired output –
1Hello, Coding Raptor!

Again, as with javac, you can specify CLASSPATH by saying java -cp or java -classpath followed by the path.

Leave a comment

Your email address will not be published.