How to Compile and Run your First Java Program

How to Compile and Run your First Java Program
Photo by nousnou iwasaki / Unsplash

Java is one of the most powerful and popular programming languages. Even with several niche languages available now, Java has retained its leading position among developers.

If you have decided to learn Java, it's definitely a good choice to enter the world of programming, but the question is, how to create your first piece of code?

Let's go through six steps of creating a Java program. We’ve also prepared bonus advice on where you can learn and practice Java.

1. Download and Install Java Development Kit

To write and compile a Java program, you will need a software development kit named Java Development Kit, or Java SE Development Kit, or just JDK for short. Here, SE stands for the Standard Edition version of the kit which is enough to compile and run your Java-written codes.

What does JDK consist of?

It includes the Java Runtime Environment, interpreter/ loader, compiler, archiver, documentation generator, and other tools required for Java development.

JRE, also called the Java Runtime Environment, is the primary core of the Java Development Kit. It includes the Java Virtual Machine(JVM) that runs Java programs by translating the instructions from bytecode (which is an intermediary step in the Java compilation process) to platform-based instructions. After that it executes commands as well as other core code such as collections, File I/O, networking and so on.

Installation steps

To download and install JDK you need to:

  • Select the version of your computer that is compatible with the version of the JDK
  • Click Accept License Agreement
  • Once you download the JDK program, you need to follow some steps and let it run some default settings while you press the Next button frequently.
  • One the JDK is installed in the C disk of My Computers, open the Command Line and type this command in such a way that the text is somehow separated (like, command -parameter).
  • If the java -version runs smoothly, but the javac -version does not, then you might need to change the environment variables of your PC from the Advanced System Settings in the System Properties section of My Computer.

2. Make a File

The following steps will provide you a detailed stepwise idea on how to make a file after you have downloaded the Java Development Kit:

Go to the My Documents section of your file explorer:

In this window, you will have to make a right click on the New and click on Text Document which will create a file named 'New Text Document'. You will be able to rename it later.

Fill the file with the text

The next step is the creation of the Hello World program as a part of the Java computer coding. This part includes the insertion of different other lines of code.

The key points of the setup and how to use them are described below.

Write the framework of your program

Class is used to state classes in the code.

Follow the steps given below:

  • Open the text document you created;
  • Inside the file, type "class MyFirstProgram {";
  • Press the Enter key 2 times and then put in the } bracket to end the program;

Setup the "main" method

Public specifies access and says that this function is shown to everybody.

Static in the line above is used to make a function steady.

Void is a return type, which means it will not return any action.

Main is the vital method in Java, which contains all the logic of the code. Here, the Java Machine is instructed to run an application by specifying its class

String[] args is an argument code which expects an array of Strings to execute.

Follow the steps given below:

  • On the second line of the program, click on the Tab key and then put in  "public static void main(String[] args) {";
  • After clicking the Enter key twice, use the } to end the program;

Write your instruction

System.out.println is used to give the instructions to the computer. In this part of the code, System means the available class, out means that we give access to the computer output. Using the printIn part will put in the inserted text to the output.

Follow the steps given below:

  • On the next empty line press the Tab key;
  • Put "System.out.println("Hello, World!");"

Now that the file is made, you need to save it.

3. Save the File as .java

We described the steps of how to save your file:

  • Select File and then choose Save As;
  • Rename the file to “”;
  • Change the field to All Files and select Save;

Now it’s time to move on to the Command Prompt.

4. Open the Command Prompt!

To open the Command Prompt you need to:

  • Open My Documents;
  • Make a new folder and name it MyFirstProgramFolder;
  • Drag and drop your "MyFirstProgram" file;
  • Right click and select Open Command Window here;

Now that you opened the right window, you need to compile the file.

5. Compile the .java File!

The necessary steps are:

  • Go to the File Explorer C:\ Program Files\ disk and open the Java folder to choose the folder beginning with "jdk";
  • Select the folder named bin;
  • Copy the file path;
  • Go back to the command prompt and insert ' " ';
  • Type in "javac";

The compile is completed. Now, let’s run the program.

6. Run the Program

That’s it, you are at the final stage. Here is what you need to do:

  • Go to the Command Prompt and click the one up arrow key;
  • Select delete and type "\java MyFirstProgram" to run the program;

Congrats! You’ve compiled and run your first Java program. A great start to your career, don’t you think? In any case, you still need to learn a lot and practice even more to be a pro.

The next paragraph will give you some tips on how to proceed with your studying.

Where to Learn and Practice More on Java

Practice makes perfect. There is no point in digging yourself in books if you don’t try what you’ve learned. So, the best courses are those that offer the opportunity to practice.

A bright example of the latter is CodeGym. This platform lets a beginner get access to both theoretical material and a variety of Java exercises. Their innovative, gaming-based and interactive manner is what makes them different from similar courses. As a nice bonus, the website allows you to steer away from the clumsy steps of compiling a program. You just have to type into the WebIDE and compile by pressing a single button.

So if you are ready to level up from newbie to a pro, you should definitely practice Java on Codegym.


We’ve thoroughly reviewed all the steps of creating a Java program. Remember that the first thing to do to compile and execute Java code is to download the JDK. The rest of the program usually follows through using the Command Prompt.

If you have more questions about Java and are eager to practice on CodeGym, check out their courses!

Read more