Using the Command-Line Tools Core Java

Let us get started the hard way: compiling and launching a Java program from thecommand line.

  1. Open a shell window.
  2. Go to the CoreJavaBook/v1ch02/Welcome directory.
  3. Enter the following commands:
    java Welcome

You should see the output in the shell window.

Compiling and running

Compiling and running

Congratulations! You have just compiled and run your first Java program.

What happened? The javac program is the Java compiler. It compiles the file Welcome .javainto the file Welcome .class. The java program launches the Java virtual machine. It executes the bytecodes that the compiler placed in the class file.

NOTE: If you got an error message complaining about the line for (String g : greeting)then you probably use an older version of the Java compiler. Java SE 5.0 introduced a numberof very desirable features to the Java programming language, and we take advantage of them here.

If you are using an older version of Java, you need to rewrite the loop as follows:
for (int i = 0; i < greeting.length; i++)

The Welcome program is extremely simple. It merely prints a message to the console. You may enjoy looking inside the program shown in Listing below

Troubleshooting Hints

In the age of visual development environments, many programmers are unfamiliar with running programs in a shell window. Any number of things can go wrong, leading to frustrating results.

Pay attention to the following points:

  • If you type in the program by hand, make sure you pay attention to uppercase and lowercase letters. In particular, the class name is Welcome and not welcome or WELCOME.
  • The compiler requires a file name ( When you run the program, you specify a class name (Welcome) with out a .java or .class extension.
  • If you get a message such as “Bad command or file name” or “javac: command not found”, then go back and double-check your installation, in particular the execution path setting.
  • If javac reports an error “cannot read: Welcome .java”, then you should check whether that file is present in the directory.Under UNIX, check that you used the correct capitalization for UnderWindows, use the dir shell command, not the graphical Explorer tool. Some text editors (in particular Notepad) insist on adding an extension .txt after every file. If you use Notepad to edit, then it actually saves it as Underthe default Windows settings, Explorer conspires with Notepad and hides the .txtextension because it belongs to a “known file type.” In that case, you need torename the file, using the ren shell command, or save it again, placing quotes around the file name: "".
  • If you launch your program and get an error message complaining about ajava .lang .NoClass DefFound Error, then carefully check the name of the offending class.If you get a complaint about welcome (with a lowercase w), then you should reissuethe java Welcome command with an uppercase W. As always, case matters in Java. If you get a complaint about Welcome/java, then you accidentally typed java Welcome .java. Reissue the command as java Welcome.
  • If you typed java Welcome and the virtual machine can’t find the Welcome class, then check if someone has set the CLASSPATH environment variable on your system. (It isusually not a good idea to set this variable globally, but some poorly written software installers in Windows do just that.) You can temporarily unset the CLASS PATH environment variable in the current shell window by typing
    set CLASSPATH=
    This command works on Windows and UNIX/Linux with the C shell. On UNIX/Linux with the Bourne/bash shell, use export CLASSPATH=
  • If you get an error message about a new language construct, make sure that your compiler supports Java SE 5.0.
  • If you have too many errors in your program, then all the error messages fly by very quickly. The compiler sends the error messages to the standard error stream, so it’s abit tricky to capture them if they fill more than the window can display.
    Use the 2>shell operator to redirect the errors to a file:
    javac 2> errors.txt

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Core Java Topics