How to Write and Compile Your First Java Program

Previously we discussed how to download and install java compiler. As a beginner, you need to practice a lot of programs to learn java programming language. The minimum requirement is a text editor program and Java SDK.

Advertisements

Learn how to install a java compiler before you begin writing your programs.

To practice writing Java programs, you must do three activities given below.

  1. Write your program source code
  2. Compile your program
  3. Execute your program to see if it is working

All java program goes through the three activities mentioned above. But, large java software projects are not so simple, they follow a software development lifecycle along with these three main activities. In other words, these activities are part of the software development process.

Let us begin writing our first Java program.

Writing Program Source Code

You can write a Java program code on any text editor, there are no special requirements. A lot of programmer like to use Java IDE (an Integrated Development Environment) such as Netbeans, Eclipse.

A Java IDE helps to add more packages and manage your project files efficiently, but the real compilation is done by Java SDK. For the sake of learning, you will interact and compile all your programs using Java SDK.

Follow these steps to writing your first java program.

Step 1: Create a directory

The first step is to create a directory called JavaProjects in any drive of your choice.

Create JavaProject Directory
Create JavaProject Directory

Step 2: Open notepad and write following Java code

The next step is to open a notepad file on your windows computer and enter the following code.

HelloWord.java File
HelloWorld.java File

Note that the public class name is HelloWorld, therefore, you must save the file as HelloWorld.java in the directory JavaProject.

Advertisements

The source code part of writing your program is completed. In the next section, you will learn to compile a java program into a format that is understandable by machines.

Compiling Java Source File

The Java compiler may not work correctly in some cases when installing for the first time. There are a few things to take care of before compiling your source code for the program.

You need to set the PATH and the CLASSPATH to making your compiling process work. The PATH let you run java command from a command prompt on a windows computer.

The CLASSPATH provides necessary resources and classes to compile and execute your program.

You can set the PATH and CLASSPATH from command line itself. Run the following command to set the values. Note that the command is case sensitive.

Set PATH and CLASSPATH
Set PATH and CLASSPATH

Now you are ready to compile your java source code into the program. Go to the project directory from the command prompt.

Make sure HelloWorld.java file is in the JavaProject directory and type the following command.

Compile Program Java
Compile Your Program with javac command.

After compilation, you may see a new file – HelloWorld.class, which is a binary file in java.

HelloWorld Class File
HelloWorld Class File

In the next section, you will execute the class file and get an output from the program.

Executing Java Program

The Java class file is executable. To run the class file stored in the JavaProject directory, open command prompt, and type following command.

Make sure that you are in JavaProject directory in command prompt.

Execute Your Program
Execute Your Program

The program prints the output successfully. You only have to remember these two commands – javac and java.

Related Articles:

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.