Making Sure Java Is Working
To make sure everything works, write a simple java program in a text editor. Then, compile and run it using cmd. You can follow the steps below:
Step 1: Write and Save Java program (HelloWorld.java)
1. Open a text editor such as Notepad++ and create a new file.
2. Enter the following lines of code and click Save:
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello world!");
}
}
3. Name the file as HelloWorld and save it as a Java source file (*.java).
Note that: For Java to recognize this as a public class (and not throw a compile time error), the filename must be the same as the class name (HelloWorld in this example) with a .java extension. There should also be a public access modifier before it.
Step 2: Compile and Run the HelloWorld.java
1. In the command prompt, change the directory to the file's location, for example, if your file location is at "C:\Users\TC\Desktop\JAVA", you can type in the following code in cmd to go to the directory.
cd C:\Users\TC\Desktop\JAVA
If for example your file is at different location which is in different drive (e.g. D:JAVA\), you need to change the drive from C: to D: by typing in the following code:
D:
Then, you can use cd to go to the directory by entering the following code:
2. Once you are in the right location of your java file, use the following syntax to compile the program:
javac HelloWorld.java
After a successful compilation, the program generates a .class file in the file directory. In this case, it will generate HelloWorld.class file.
3. Run the program with the following syntax:
java HelloWorld
The output shows that the program runs correctly, displaying the Hello world! message. You can refer to the picture below for reference.