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.

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 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 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 3

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

5 ★ | 1 Vote

May be interested

  • Why write neat and organized HTML?Why write neat and organized HTML?
    you will get many benefits from writing clean and precise html code. here's why it's a good idea to write optimal html code.
  • eQuiz - Multiple choice quiz about JAVAeQuiz - Multiple choice quiz about JAVA
    following are multiple-choice questions related to java programming language, there will be 23 questions in total with no answer for each time. let's get started!
  • Test on JAVA test on P9Test on JAVA test on P9
    unlike most common programming languages, instead of compiling the source code into machine code or interpreting source code when running, java is designed to compile the source code into bytecode, then bytecode will be run by the runtime environment. . to help you better understand this programming language, the following quiz of network administrator will give you lots of useful knowledge.
  • How to Get Rid of Java Update NotificationsHow to Get Rid of Java Update Notifications
    this wikihow teaches you how to prevent java from sending update notifications every time you log into your computer. open the java control panel. to do so:
  • How to install Java on the computerHow to install Java on the computer
    how to install java on the computer. currently, there are still a lot of applications running on the computer on java, so installing java on the computer is a must. for example, those of you who are accountants who regularly have to file electronic tax returns are definitely ph
  • 9 things Java programmers should know in 2018 if they want a successful career9 things Java programmers should know in 2018 if they want a successful career
    you want to start the new year in a right direction? join tipsmake.com to consult 9 things java programmers should know in 2018 if you want a successful career!
  • Guide the most simple and effective way to write easy-to-read codeGuide the most simple and effective way to write easy-to-read code
    invite you and tipsmake.com to find out the article to guide the simple and most effective way to write easy-to-read code below!
  • How to fix the error does not install JavaHow to fix the error does not install Java
    more and more applications and websites require java installation before use. unfortunately, sometimes you can't install java or install it, but it doesn't work. the article will provide ways to fix errors that do not install java on your computer.
  • How to Download EclipseHow to Download Eclipse
    eclipse is a open-source application made by the eclipse foundation to help you write java better and it has become the most popular java editor. if you are having trouble getting eclipse for your system here's how you do it. head to the...
  • 3 ways to play Java games on your computer3 ways to play Java games on your computer
    if you love java games and mobile apps and want to run them on your computer, try one of the following three emulator software. the emulator software allows users to run .jar files for mobile phones on computers.