Compiling and Running Java: JDK
Problem
You need to compile and run your Java program.
Solution
This is one of the few areas where your computer’s operating system impinges into Java’s portability, so let’s get it out of the way first.
JDK
Using the command-line Java Development Kit (JDK) may be the best way to keep up with the very latest improvements from Sun/JavaSoft. This is not the fastest compiler available by any means; the compiler is written in Java and interpreted at compile time, making it a sensible bootstrapping solution, but not necessarily optimal for speed of development. Nonetheless, using Sun’s JDK (or Java SDK), the commands are javac to compile and java to run your program. For example:
C:\javasrc>javac HelloWorld.java C:\javasrc>java HelloWorld Hello, World C:\javasrc>
As you can see from the compiler’s (lack of) output, this compiler works on the Unix “no news is good news” philosophy: if a program was able to do what you asked it to, it shouldn’t bother nattering at you to say that it did so. Many people use this compiler or one of its clones. The javac and java commands are available with the JDK on both Windows and Unix, and under MacOS X if you have installed the bundled Developer Tools package.
There is an optional setting called CLASSPATH, discussed in Section 2.6, that controls where Java looks for classes. CLASSPATH, if set, is used by both javac and java. In older versions of Java you had to set your CLASSPATH to include “.” even to run a simple ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access