Thursday, May 14, 2020

How To Run Java Program Code (Compile Process)


how_to_run_java_program

There are 2 choices for writing Java program code, using a text editor or using an IDE (Integrated Development Environment).

Text editor is an application specifically designed for writing text. A text editor is mostly lightweight and offers standard features such as line numbering and syntax highlighting. Generally, text editors can be used to write various programming languages.

Text editor of choice is quite varied, among which is quite popular is Notepad ++, Sublime Text, Atom, and VS Code. Windows Notepad is a text editor, but it is not suitable to make a program because it is too simple, for example it does not have line numbering.

Read also: How To Compile And Run Java Programs Using Visual Studio Code

Another option for creating a Java program code is to use an IDE (Integrated Development Environment). An IDE can be said to be a heavyweight text editor because it offers many features. So the IDE file size is much larger than an ordinary text editor.

The majority of IDEs are specifically designed for certain programming languages, although some provide features for other programming languages. Popular IDE choices for the Java language are Eclipse, NetBeans, and IntelliJ IDEA. One of the features of the IDE is writing, running, and inspecting Java program code errors in one place.

Java IDEs such as Eclipse or NetBeans is useful for advanced material. So for today's Java learning tutorial, I decided to use the traditional method first, using a text editor. The goal is that we can see how to compile and run Java code manually and find out what is being run.

How To Write Java Program Code


To be more organized, I will create a special folder called
learning_java on drive D. All tutorial files will be placed within this folder. But, this is not mandatory, you can place Java files anywhere.

Next, run any text editor and create a new file. Then, please type the following program code or just copy and paste:

class Hello {
  public static void main(String args[]){
    System.out.println("Hello World");
  }
}

For the time being, we will not discuss the contents of the program code above. The main goal now is only to see how to run the Java program code. But, the point of the program code above is that I want to display the text "Hello World".

After writing the above program code, save it into the D:\learn_java folder with the file name Hello.java.
save_file_hello.java

How To Compile Java Program Code (javac.exe)


Navigate to the D:\learn_java folder by typing the following command:

D:

cd learn_java

Once inside the learn_java folder, type the following command:

javac.exe Hello.java

Here, we run the javac.exe file with the Hello.java as the input file. That is, the Hello.java file will be read and processed by the javac.exe application. If no problems are found, the javac.exe application will create a Java byte code with the name Hello.class.

Please open Windows Explorer and enter the D:\learn_java folder, there will be 2 files: Hello.java and Hello.class.

Hello.java_and_Hello.class

Up to this point, we have successfully compiled the Java program code using javac.exe.

Running Java Byte Code (java.exe)


To show the output results, we must run the Hello.class file using the java.exe application.

So, let's open the Windows cmd and navigate to the learn_java folder. Now type the following command:

java.exe Hello

This command instructs the java.exe file to look for the Hello.class Java byte code file in the current folder and run it. Note that we only need to write "Hello", even though the file name is "Hello.class".

As a result, the text "Hello World" will appear as shown below:

run_java_byte_code

Congratulations! You have successfully written, compiled, and run a Java program code.

To summarize, here is the process we have made:
  • Write the Java program code using a text editor, then save as Hello.java.
  • Run the javac.exe application to compile the Hello.java program code into a Java byte code with the name Hello.class.
  • Run the java.exe application to run the Java byte code Hello.class. The result is the text "Hello World" will appear in Windows cmd.
In this tutorial, we have discussed how to run the Java program code. But when making program code, there are times when there are typos in the command. What if the error occurs? We will discuss in the advanced Java tutorial on this blog: Learn Java Program Code Error Messages.