Java compile FAQ: How to compile a Java program

Java compile FAQ: How do I compile my first Java program?

To compile a Java program you first need to download a Java development kit (see java.sun.com) if you don't already have one. Next, assuming you have a program named MyProgram.java that you want to compile, you compile it like this with the javac:

javac MyProgram.java

Assuming that there are no errors, this will create a file named MyProgram.class. This file contains the Java bytecode representing the instructions of your program.

Once you've compiled your Java program, you can execute (run) the program using the Java interpreter, also known as the Java Virtual Machine (JVM), like this:

java MyProgram

Okay, you're thinking, that seems pretty easy ... anything else I should know?

Yes, learn the options of the javac and java commands. One option common to both is the -verbose option. Here's an example:

java -verbose MyProgram

Seeing through this output will help you understand what the JVM is doing for you.

 

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.