This write-up will provide a detailed understanding of the below-given concepts:
- How to create a simple program in java.
- How to compile a program in java.
- How to run a program in java.
Before heading toward the java coding, we recommend you to visit the following link to understand the basic syntax of the java programming language.
How to create a simple program in java
Let’s create a very basic java program to print a greeting message. Type the below-given command on any text editor like notepad, notepad++, or any IDE like NetBeans, Eclipse, etc.
public static void main(String[] args){
System.out.println("Hello! Welcome to linuxhint");
}
}
Save the program as “SimpleJavaProgram” and provide the extension “.java”. Must remember the “public class name” and “filename” should be the same. Otherwise, you will encounter an error.
How to compile a program in java
Now, let’s move a step further to learn how to compile a java program.
First Step
To do so, open the windows command prompt and type the below mentioned command to enter the directory where the java program is saved:
Here “Java Program” is the directory name where our java program is located.
Pressing the enter button will guide you to the specified directory.
Second Step
Now, type the below command to compile the java program:
Type the above command and press the enter button, if the cursor moves to the next line without showing any error, it means you have successfully compiled your java program as shown in the below snippet:
The cursor moves to the next line to show that our program is compiled successfully.
How to run a program in java
Type the below-given command on the windows command prompt to run a java program:
Following will be the output:
This is how a program can be compiled and run through a command prompt in the Windows operating system.
Conclusion
We can write the code for our java program on any text editor like notepad, notepad++, etc. or any IDE like NetBeans, Eclipse, etc. To compile a java program, type “javac fileName.java” on the windows command prompt while to run the java program, type “java fileName” on the command prompt and press enter. This write-up explained how to create, compile, and run a program in java.