Compiling Java code
Compiling code on the command-line
As discussed on the first slide, all the code is contained in the workspace
you specified in the home directory. If you followed the example image,
your workspace is a folder called CMSC202
in your home
directory.
Browse to your project folder (LabAssignments
), then the source code
folder (src
). In your source folder should be a folder called lab2
.
The example below illustrates how to compile your lab2 code directly from a UNIX terminal.
We use thejavac
command to compile the contents of the lab2
directory.
javac -d . lab2/*.java
To execute your code, use the command
java lab2.ScannerInput
Important Note: Make sure you run the above two commands from the "src" directory. Running them from any other folder might cause unexpected errors like the ones shown below:
linux1[29]% javac -d . lab2/*.java javac: No match. linux1[30]%OR
linux1[27]% java lab2.ScannerInput Exception in thread "main" java.lang.NoClassDefFoundError: lab2/ScannerInput Caused by: java.lang.ClassNotFoundException: lab2.ScannerInput at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: lab2.ScannerInput. Program will exit. linux1[28]%