Article

Getting Started with Java

Page: 1 2 3 4

Compiling and Running your Program

With your first Java program written, you're probably anxious to see it running. Before you can run your program, you must first convert it into a format that the Java Virtual Machine (JVM) can understand. This process, called compilation, involves running a program called the Java compiler that checks your Java code for mistakes (missing semicolons and whatnot) and then converts it into a binary format called Java bytecode that the JVM can understand. The Java compiler is run from the command line by typing javac, followed by the name of the Java code file that you want to compile.

To compile your Hello.java file, start by opening a command prompt and navigating to the directory containing the file to be compiled. On Windows, for example, to navigate to D:\MyFirstJava, type the following commands (hitting Enter after each):

C:\> d:    
   
D:\> cd MyFirstJava    
   
D:\MyFirstJava>

To compile Hello.java, type the following command:

D:\MyFirstJava> javac Hello.java

If everything goes as planned, there should be a short delay followed by another prompt (no news is good news!). If the compiler finds anything wrong with your code, however, one or more errors or warnings will be displayed. In most cases, the exact line number and some clue as to the nature of the error will be given. Compare your code to that provided above and make sure they match exactly (remember, the line numbers are not part of the code).

Once you've successfully compiled your program, a file called Hello.class will have been created in the directory alongside your Hello.java file. This is the compiled version of your Java code, and contains a description of the Hello class that you defined in your file. Such files are called class files, for obvious reasons. That description is in binary format (specifically, in Java bytecode), and is ready to be run by the JVM.

Running a Java program is similar to compiling it, except that instead of using the Java compiler (javac), you use the Java Runtime Environment (JRE), which you can invoke using the java command, followed by the name of the class that contains the special main method. In this case, your class is called Hello, so to run your program you type the following command:

D:\MyFirstJava> java Hello
Hello, World!
This is a test.

As expected, the two messages, Hello, World! and This is a test. are printed out. Your program is working! One important note: when running a program, you need to specify the name of the class (Hello), not the name of the class file (Hello.class). Java finds the class file automatically using the name of the class. If you told it to run Hello.class, it would be looking for a file containing a definition of a class called Hello.class, and you'd get an error like the following:

D:\MyFirstJava> java Hello.class
Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class

This NoClassDefFoundError indicates that Java couldn't find a definition of the class it was looking for, in this case Hello.class (which it displays as Hello/class for reasons that I'll explain in a future article).

Summary and Further Reading

In this article, I've provided an introduction to the Java programming language from the ground up. I covered the basics of what Java is and why it's a useful tool to have under your belt, I explained what tools you need to write, compile, and run Java programs on your computer and where you can get them, and finally I walked you through the process of creating and running a very simple Java program.

As I said, this is only the first in a series of articles intended to bring you into the world of Java programming for the Web. In part 2, Java Language Baics, we'll explore the language in greater detail, covering data types, operators, variables, and flow of control. In the meantime, if your ultimate goal is to learn to create dynamic Web sites using Java Servlets and JavaServer Pages (JSP), you can read my JSP Quick-Start Guide to set up the software you'll need to produce server-side Java programs for the Web.

If you're anxious to move forward with the Java language, there are some other resources you can refer to while awaiting additional instalments in this series. The Java Tutorial is the official online guide to learning most aspects of Java, and can be accessed free of charge. A better way to learn the language, however, is to pick up a good book on the subject. My best recommendation goes to "Beginning Java 2 – JDK 1.3 Edition" from WROX Press, and a complete review can be found here.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: