Back to: Java Tutorials For Beginners and Professionals
Creating First Java Program
In this article, we are going to discuss Creating First Java Program and we will also discuss how to compile and run Java Program. Please read our previous article, where we discussed the Environment for Java in the Windows Operating System. You can use Text Editor as well as Eclipse IDE to run the Java Program.
Simple Hello World Program in Java
public class FirstProgram { Public static void main(String args[]) { System.out.println(“Hello World”); }//End of Main }//End of class
Output: Hello World
To Compile and Run the Above Program
Step-1: Open the Text Editor like Notepad, then Copy and Paste the above code into the text editor.
Step-2: Save the file as FirstProgram.java. You may be wondering why we have named the file as FirstProgram, the thing is that we should always name the file the same as the public class name. In our program, the public class name is FirstProgram, that’s why our file name should be saved as FirstProgram.java
Step-3: Now we will Compile the program. To compile the program, open your Command Prompt and type javac FirstProgram.java, and press Enter.
Note: You may get this error when you try to compile the program: “javac” is not recognized as an internal or external command, operable program, or batch file. This error occurs when the java path is not set in your system. In this case, you first need to set the Path before compiling your program.
Set Path in Windows:
Open cmd, copy the complete path where you have installed java in your system and write it in cmd.
Note: This path setting is temporary, when you close the cmd you will lose the path setting and you need it to set it again when you want to run the code.
set path=C:\Program Files\Java\jdk1.8.0_121\bin
Step-4: After compilation the .java file gets translated into the .class file(byte code). Now we can run the program. To run the program, type the following command and press enter:
java FirstProgram
Note: You don’t need to append .java while running the code.
A closer look at the First Java Program
public class FirstProgram{
This is the first line of our java program. Every java application must have at least one class definition that consists of a class keyword followed by the class name. When I say the keyword, it means that it should not be changed, we should use it as it is. However, the class name can be anything.
I have made the class public by using the public access modifier, I will cover the access modifier in a separate post, all you need to know now is that a java file can have any number of classes but it can have only one public class and the file name should be same as the public class name.
public static void main(String args[])
Where,
- public: This makes the main method public which means that we can call the method from outside the class.
- static: We do not need to create an object for static methods to run. They can run themselves.
- void: It does not return anything.
- main: It is the method name. This is the entry point method from which the JVM can run your program.
- (String[] args): Used for command-line arguments that are passed as strings. We will cover that in a separate post.
System.out.println(“Hello World”);
This method prints the contents inside the double quotes into the console and inserts a newline after.
Create, Compile and Run Java Program in Eclipse IDE
Step-1 : Select File -> New -> Java Project from the Menu. Enter the project name as “MyFirstProject” and press the Finish button to create the project as shown in the below image.
Step-2: Now Create Package. Right-click on the Java Project you created and select New -> Package from the menu. Enter the Package name as “Demo” and click on the Finish button as shown in the below image.
Step-3: Now we will create our first class. Right-click on the package and select new -> Class. Enter the class name as “FirstProgram”, and select the “public static void main(String args[])” checkbox, and click on the Finish button as shown in the below image.
This creates a new file and opens the Java editor where you can write your first Java Program as shown in the below image.
Step-4: Now run your code. Either right-click on your Java class in the Package Explorer or right-click in the Java class and select Run-as Java application.
The eclipse will run your Java program. You should see the output in the Console view.
Congratulations! You created That’s it. You successfully created your first Java project, a package, and a Java class and you ran this program inside Eclipse.
In the next article, I am going to discuss Data Types in Java Applications with examples. Here, in this article, I try to explain, How to Create the first Java program as well as how to compile and run the java program step by step. I hope you enjoy this Creating the first Java program article.