This document instructs you on how to use the Linux shell with Java.
For a light-hearted essasy on the command line, you might enjoy
Neal Stephenson's light-hearted
essay In the Beginning was the Command Line.
|
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. We'll assume you have already installed these.
|
You will type commands in an application known as the shell. Since you're using Linux, we assume you're somewhat familiar with it.
Your shell will likely be bash, tcsh, sh, or ksh.[wayne] ~> echo $SHELL
Then close your shell and open up a new one. If it does not work, in addition, try repeating the same instructions with the file /u/username/.bash_profile.export PATH=$PATH:~/j2sdk1.6.0.18/bin
Then close your shell and open up a new one.setenv PATH ~/j2sdk1.6.0.18/bin:$PATH
Then type the following javac command.[wayne] ~> java -version java version "1.6.0_18" Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0_18-112) Java HotSpot(TM) Client VM (build 1.6.0_18-64, mixed mode, sharing)
[wayne] ~> javac -version javac 1.6.0_18 javac: no source files ...
|
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
[wayne] ~> cd introcs/hello [wayne] ~/introcs/hello>
If everything went well, you should see no error messages.[wayne] ~/introcs/hello> javac HelloWorld.java [wayne] ~/introcs/hello>
|
You will use the java command to execute your program.
If all goes well, you should see the output of the program - Hello, World.[wayne] ~/introcs/hello> java HelloWorld Hello, World
Input and Output |
If your program gets stuck in an infinite loop, type Ctrl-c to break out.
If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file). You should type this character on its own line.
|
When I try to run java I get: Exception in thread "main" java.lang.NoClassDefFoundError. First, be sure that HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Also, try running your program with the command line
If this works, your classpath is set incorrectly.[wayne] ~/introcs/hello> java -cp ./ HelloWorld
How do I check that my PATH variable is set correctly? Type echo $PATH.
Where can I learn more about the command line? Here is a short tutorial on the command-line.