The most transparent way to create, compile, and run a Java program is to use an ordinary text editor to create a file called Demo.java. Here are the contents of the file:
public class Demo {
public static void main(String[] args)
{
// your code goes here:
System.out.println("Hello
World!");
}
}
This is a Java class declaration. The name of the class is Demo. This means the name of the file must be Demo.java. Note that Java is case-sensitive.
In order to execute a class from the command prompt, the class must contain a public static method called main. Furthermore, main has a void return type and a single argument, args, which is an array of strings. If you don't understand the terminology, don't worry about it. For now you can simply copy this code, paste it into your file, change the name of the class, then replace the implementation of main with anything you like.
Note also the formatting of Demo. It follows the Java style conventions.
It's a good idea to keep all of your Java programs under the same directory. For example:
c:\smith\java\labs\Demo.java
From a command prompt set the working directory to the directory containing Demo.java, then type the command:
javac Demo.java
If you're lucky, nothing will happen:
Actually, a lot happened. javac is the name of the Java compiler. It translates Java into Java Bytecode, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file called Demo.class.
Next, type the command:
java Demo
java is the name of the Java Virtual Machine (JVM). A virtual machine is a computer implemented in software. Executing programs with a virtual machine is a bit slower than executing them with a physical machine, but the advantage is that we can run the same program on any physical machine so long as a virtual machine is available:
The JVM searches for the file Demo.class. (Note that you do not type the extension.) If it finds it, it loads it, then searches for a static method called "main". If it finds it, it executes the instructions inside of main. In our demo there was a single instruction:
System.out.println("Hello World!");
System is the name of a library class that represents the underlying physical machine. It is assumed that among other things the physical machine has a standard input stream and a standard output stream: System.in and System.out, respectively. It is further assumed that the output stream has the ability to print strings followed by a newline character. This is achieved by the method System.in.println:
The following diagram summarizes the steps:
Check
C:\Program Files\Java\jdk*\bin
If this directory can't be found, then probably you have not downloaded and installed the JDK. Most computers come equipped with some version of the JRE (Java Runtime Environment), but this is only the JVM, not the compiler.
If this directory can be found, then make sure it is on the Path environment variable:
If not, then add it using the System/advanced control panel:
List the working directory. Do you see it? Remember, javac is case sensitive and you must specify the extension.
The JVM uses an environment variable called classpath to search for classes. In some cases the current directory (.) must be on the classpath:
You may need to add . to the classpath using the System/advanced control panel.
Does your class have a main method? Does the first line of main exactly match the one above?