Assuming that the "HelloWorld.java" contains the following Java source:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
(For an explanation of the above code, please refer to Getting started with Java Language .)
We can compile the above file using this command:
$ javac HelloWorld.java
This produces a file called "HelloWorld.class", which we can then run as follows:
$ java HelloWorld
Hello world!
The key points to note from this example are:
HelloWorld
. If they don't match, you will get a compilation error.java
, you supply the classname NOT the bytecode filename.Most practical Java code uses packages to organize the namespace for classes and reduce the risk of accidental class name collision.
If we wanted to declare the HelloWorld
class in a package call com.example
, the "HelloWorld.java" would contain the following Java source:
package com.example;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
This source code file needs to stored in a directory tree whose structure corresponds to the package naming.
. # the current directory (for this example)
|
----com
|
----example
|
----HelloWorld.java
We can compile the above file using this command:
$ javac com/example/HelloWorld.java
This produces a file called "com/example/HelloWorld.class"; i.e. after compilation, the file structure should look like this:
. # the current directory (for this example)
|
----com
|
----example
|
----HelloWorld.java
----HelloWorld.class
We can then run the application as follows:
$ java com.example.HelloWorld
Hello world!
Additional points to note from this example are:
If your application consists of multiple source code files (and most do!) you can compile them one at a time. Alternatively, you can compile multiple files at the same time by listing the pathnames:
$ javac Foo.java Bar.java
or using your command shell's filename wildcard functionality ....
$ javac *.java
$ javac com/example/*.java
$ javac */**/*.java #Only works on Zsh or with globstar enabled on your shell
This will compile all Java source files in the current directory, in the "com/example" directory, and recursively in child directories respectively. A third alternative is to supply a list of source filenames (and compiler options) as a file. For example:
$ javac @sourcefiles
where the sourcefiles
file contains:
Foo.java
Bar.java
com/example/HelloWorld.java
Note: compiling code like this is appropriate for small one-person projects, and for once-off programs. Beyond that, it is advisable to select and use a Java build tool. Alternatively, most programmers use a Java IDE (e.g. NetBeans, eclipse, IntelliJ IDEA) which offers an embedded compiler and incremental building of "projects".
Here are a few options for the javac
command that are likely to be useful to you
-d
option sets a destination directory for writing the ".class" files.-sourcepath
option sets a source code search path.-cp
or -classpath
option sets the search path for finding external and previously compiled classes. For more information on the classpath and how to specify it, refer to the The Classpath Topic.-version
option prints the compiler's version information.A more complete list of compiler options will be described in a separate example.
The definitive reference for the javac
command is the Oracle manual page for javac
.