Write and run Java code on the computer for the first time

After you've installed Java and set up a path on your computer, you need to know how to run a simple Java code on your computer. In this article, we will try to write the first Java program, as well as the commands to run this program in cmd.

After you've installed Java and set up a path on your computer, you need to know how to run a simple Java code on your computer. In this article, we will try to write the first Java program, as well as the commands to run this program in cmd.

Be sure to read and follow the instructions in this article so that the process of running the code won't fail: Download and install Java on the computer.

The first Java program

Yes, the most classic code when learning every major programming language is Hello or Hello World.

 public class JavaFirst { /* Đây là chương trình Java đầu tiên của tôi. * Nó sẽ in 'Hello World' ra màn hình */ public static void main ( String [] args ) { System . out . println ( "Hello World by Quantrimang" ); // in Hello World } } 

Do not wonder if you do not understand anything, just copy the one, then it will be fine.

Write and run Java code on the computer for the first time Picture 1Write and run Java code on the computer for the first time Picture 1

Run Java code on the computer

Now see how to save the file, compile and run the Java program, make sure you follow the steps below:

  1. Make sure you have installed Java JDK and tweaked PATH on the computer.
  2. Open Notepad and paste the code above.
  3. Save the file as: JavaFirst.java
  4. Open cmd and go to the directory where you saved the JavaFirst.java file, assuming cmd is in drive C, you save the file in folder E: Java, enter the following command to switch to the folder E: Java.
 cd d / E: Java 

Write and run Java code on the computer for the first time Picture 2Write and run Java code on the computer for the first time Picture 2

5. To compile the Java code you entered:

 javac JavaFirst.java 

Then press Enter. If there are no errors in the code, cmd will take you to the new line. Now if you only install Java JDK without setting PATH, you will receive an error message. If the file name you set is different (other than the class name), you will receive an error message.

6. To run the program, enter the following command:

 java JavaFirst 

Hello World by TipsMake.com will appear on the screen

The whole process is as follows:

Write and run Java code on the computer for the first time Picture 3Write and run Java code on the computer for the first time Picture 3

In the next article we will learn about Object and Class in Java.

5 ★ | 1 Vote