Three ways To Create And Run Java Application

Prerequisites

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

Create Java Project

  • Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… -> Java Project.
  • Click Next.
  • Choose any project name you want then click Finish.

No.1 Generate main Method While Creating Class

Step 1 Create New Java Class

  • Right click to the project; select New -> Class.
  • Enter any name you want in Name field.
  • Check public static void main(String [] agrs) option under Which method stubs would you like to create? to add the main method stub to the new class.
  • Click Finish.

Step 2 Write Your Code in Main Method

Step 3 Run The Application

Right click to the class file; select Run As -> Java Application. Or simply click the green play button on top toolbar.

Output

No.2 Generate main Method after Creating Class

Step 1 Create New Java Class

  • Right click to the project; select New -> Class.
  • Enter any package name in Package field such as info.java.tips.
  • Enter any name you want in Name field.
  • Click Finish.

Step 2 Write Your Code

  • Place the cursor between the class body (between the curly braces {})
  • Press CTRL + SPACE to show Template Proposals. Type m; choose main – main method then press Enter or double click on it. The main method will be generated.
  • In the main method; type sysout then press CTRL + SPACE. Eclipse will generate the code System.out.println();. Complete the code as follows:

Press CTRL + SHIFT + F to re-format the code.

Step 3 Run The Application

Right click to the class file; select Run As -> Java Application. Or simply click the green play button on top toolbar.

Output

No.3 Create Java Package & Class From Clipboard

Step 1 Copy The Code To Clipboard

Let say you go to javatips.info website then copy the code bellow to clipboard by selecting the code then pressing  CTRL + C:

Step 2 Paste The Code To Eclipse

Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically generate package & class file for you.

Step 3 Run The Application

Right click to the class file; select Run As -> Java Application. Or simply click the green play button on top toolbar.

Output