How to create and run a java first program
Integrated Development Environment for Java
Like HTML, C/C++ Java also need an editor to write and edit codes. In case you don’t have one, then you can simply use notepad. and compile and run the java programs from command prompt.
This system needs you to remember the commands and you need to set the class path if it is not set automatically during installation.
Writing a Simple Java Program
Let us write a simple Java program in notepad.
// A Simple Java Program.
class MyPrg
{
public static void main(String args[])
{
System.out.println(“A simple Java Program”);
}
}
Naming a Java File
Once you complete typing this program in notepad, save the file in a folder, file name must match the name of class (preserving the case) with extension “.java” e.g. save it as “MyPrg.java”.
Compile a Java Program
Now go to command prompt. Change the directory to the path where java code file has been saved.
To compile a java program type “javac MyPrg.java” and hit enter key.
Again cursor will get back to command prompt without any message if compilation is successful. otherwise it will return some error.
A class file has been created by the compiler by the name “MyPrg.class”. This is bytecode which are machine independent and needs to be interpreted by the appropriate interpreter.
To Interpret/run Java Program
To interpret the class file, type the command as under:
“java MyPrg” and hit enter key on the command prompt.
It will show the result.
Writing a Java Program using Eclipse Editor
Another option is to use specially designed IDE, such as Eclipse. Eclipse is very famous IDE used for java, C/C++, PHP and many more languages. It has JRE inbuilt so that programs can be directly compiled and run from within the same IDE. Eclipse can be downloaded from eclipse.com
Writing and Compiling Java Program using Eclipse Editor
- Open eclipse editor and click on file > new > Project
- Type the name of the project , select the JRE environment version to execute the java project and click finish.
- Now right click on the src folder on the left side of project window. Click new > select class
- Type the name of the class
- From which method stub would you like to create, Select the checkbox “public static void main(String[] args)” and click finish.
Once you finish typing java codes, Simply click on run icon or Control+F11 keys from keyboard.
More detailed insight about the topic is available on Run a Java Program