Sunday, March 23, 2014

Eclipse , Part 4 - First Java Program In Eclipse

In Eclipse Series, Part 3 , I have explained about Eclipse User Interface Used For Java Development. 
Here, I will explain how to create first new java program in eclipse

Create Project
Select FileNewJava project from the menu. Enter  as the project name. Select the Create separate folders for sources and class files flag.
Following this interface you will get new window as follow :

Project name : here we write the relevant name of project we want to create.

checking the default location point to the workspace we have chosen in eclipse startup.

JRE : this blocks point to the jre installed in our system, we can change it using drop down but the chosen jre should be available in our local machine.

A radio button to choose folders for .java file and generated .class file.

                                                           




Press the Finish button to create the project. A new project is created and displayed as a folder. Open the in.technoscience.first folder in specified workspace and explore the content of this folder. 

Create Package 
A good convention for the project and package name is to use the same name for the top level package and the project. For example, if you name your project in.technoscience.first you should also use in.technoscience.first as the top-level package name.  


To create the in.technoscience.first  package, select the src folder, right-click on it and select NewPackage










Enter the name of your new package in the dialog and press the Finish button .




Reverse domain names should be used for packages to prevent name clashes. like if you are working in XYZ company then you should use com.XYZ.first

click on the finish button and you can check the specified package name under src folder of your specified project.







Create java class 
Create a Java class. Right-click on your package and select NewClass




After clicking as specified you will get a new window as follow:






In the Name input box you need to specify the name of the class you want.


you can click on the public static void main(String[] args) option , it will let your class have main method.
click on finish button , you will get a Abc class in java editor portion of eclipse as follows.



 Run your project in Eclipse 
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-asJava application.
After clicking on Run-asJava application java runtime environment will run your .class file and then you can see your program's output in console window as follow.
here you get the output of your first program, but you must be wondering how you can find the .class file here.
you have two options:
  1. you can check your workspace directory in your local machine and check your project folder there you can find .setting , src and bin folder. you can check your bin folder there you will find your generated .class file.
  2. even you can find your .class file in eclipse as follow:



click on Window  ==> show view ==> navigator.

















After clicking on navigator you will find a new view in java perspective as follow:
But remember if you will try to open .class file in normal text editor application or eclipse it will not open , to see the content of .class you need to use decompiler and to use decompiler you can follow my post here

No comments:

Post a Comment