How to create and run a java first program

Sanjiv Kumar
3 min readMar 12, 2021

--

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.

compile and run a java program using console
Compile and run java program using console

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
Naming java class using eclipse ide
Java Clas Name
  • 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.
Writing First Java Program using eclipse ide

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

--

--

Sanjiv Kumar
Sanjiv Kumar

Written by Sanjiv Kumar

0 Followers

Blogger, Digital Marketer, Computer Trainer, Affiliate Marketer. Microsoft Certified System Engineer, HP Star Certified Professional for Netservers. BCA, MSCIT,