Create, compile and run java sample program
When we are starting learning any of the new technology, we usually starts with a sample program called Hello world. In the current post I am going to explain how to create sample hello world program and execute it in java. As we are in initial stage of learning and practicing, initially will use notepad and command prompt to create program and execute and then will look how to use IDE(Eclipse).
I am using notepad to write the program code and using command prompt to execute sample java program. My sample java program name as Hello.java.
I am using notepad to write the program code and using command prompt to execute sample java program. My sample java program name as Hello.java.
Create Hello.java program:
Open
the Notepad ---> File ---> New ---> write
the below code,
1 2 3 4 5 6 7 8 9 | public class Hello { public static void main(String[] args) { System.out.println("Hello! How are you?"); } } |
Save the file with the name as Hello.java. So, program creations completed and then next process is to compile our Hello.java program through command prompt. To compile the .java program through the command prompt, follow the below lines
Open
the command prompt, type the cd\ that removes the normal path and displays the
below one.
Go
to Hello.java path which directory you have saved the file by using the cd command
to change the directory
Compile Hello.java program:
After
you came into the Hello.java file directory path in command prompt, we need to
compile Hello.java program for checking the program is correct or any mistakes
we have done in the program. For compilation java provides a command called
javac, use javac command and compile the Hello.java like below,
If
the Hello.java file successfully completes, command prompt will display the
common path. Otherwise it will show you the mistake what we have done in the Hello.java,
Execute Hello.java program:
Once
compilation has done successfully, we need to execute our Hello.java for
getting the output of the same. For execution of java program java
provides a command called java. By using the command called java we will
execute our java program as like below,
While
executing Hello.java program JVM doesn't find any problems, that will gives you
the below output. Otherwise it will show you the problem in console.
Hello! How are you?
Note: we will declare any as Program name
except predefined Class/Interface names, but the Starting letter should be
capital. To know about the naming conventions in java, Click Here.
How to do the same application in Eclipse
IDE?
In
the above line we have seen how to create, compile and run the java program by
using Command prompt. Here we will come to know how to do the same in Eclipse
IDE.
Create project in Eclips IDE:
File
--> New --> Java Project --> <Enter name of the project> -->
Next --> Finish
Create Hello.java(Class) program:
Right
Click on SRC --> New --> Class --> <Enter Name as
"Hello"> --> Finish. Write
the below content in between Class starting and ending braces.
1 2 3 4 5 | public static void main(String[] args) { System.out.println("Hello! How are you?"); } |
If any compilations problems are there
in the program, it will show red color mark in eclipse. We no need to worry
about the class compilation using command prompt. Once completed the writing of
the program, follow the below instructions to run the program,
Right
click on the program --> Run as --> select Java Application
If
the program doesn't contains any problems that will show the output in the
console.
Comments
Post a Comment