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.
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:
- Make sure you have installed Java JDK and tweaked PATH on the computer.
- Open Notepad and paste the code above.
- Save the file as: JavaFirst.java
- 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
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:
In the next article we will learn about Object and Class in Java.
You should read it
- Basic Java exercises, with sample decoding
- 4 ways to write multi-threaded code in Java
- Basic Java syntax
- What is Java? Why choose Java?
- How to Compile a Java Program
- Download and install Java on the computer
- How to Fix Java
- Which career Java programming options are waiting for you?
- How to fix the error does not install Java
- How to Install the JDK (Java Development Kit) on a Mac
- 9 things Java programmers should know in 2018 if they want a successful career
- What is JAVA file? How to open, edit and convert JAVA files
Maybe you are interested
Thousands of iOS apps could be at risk because of an open source vulnerability The strange forest area looks like a chessboard when viewed from above Impressive image: ISS International Space Station flies over the Sun. Russian spacecraft 'resupply' successfully 3 tons of cargo for the ISS in just 3 hours How clean are astronauts keeping the space station? There will soon be a 'green vegetable garden' on the International Space Station (ISS)