Google+

65. Run Java Programs in Eclipse IDE







Pre-requisites:  Create a Java Project in Eclipse IDE by following the Post#64 How to create projects in Eclipse IDE 

Before Creating and Running Java Programs in Eclipse IDE, I would like to explain the default items that are displayed under the Created Java Project.

Default Items under a Java Project -

1. View the Package Explorer tab in 'Eclipse IDE' and ensure that 'First Project' java project created in Post#64 is available as shown below:



2. Expand the 'First Project' java project as shown below and observe that 'src' folder & JRE System Library are displayed.



3. 'src' folder is called a source folder as it contains the projects source code. Hence all the java programs we are going to write will be created as Java Class files under this source folder 'src'

4. Expand the 'JRE System Library' as shown below and observe that it contains all the predefined libraries required to create and run the Java programs - (Predefined libraries contains set of predefined Java System Classes containing predefined methods which are categorized by Packages) - JRE System Library (Java Run Time Environment Library) - This JRE System Library got installed when we have installed Java  in our system (JDK or JRE).



Create a Package -

1. Right click on the 'First Project' folder in the 'Package Explorer' and select 'New' -> 'package' as shown below  (What is the purpose of  'packages' will be explained in future posts, for now blindly create a new 'package')



2. Ensure that the 'New Java Package' dialog is displayed, type package name as 'first_package' into the 'Name' field and click on 'Finish' button



3. Right click on the newly created  package and select  'New' -> 'class' as shown below


4. Ensure that 'New Java Class' dialog is displayed, ensure that 'first_package' name is displayed by default in the 'package' field, type 'Sum' into the 'Name' field , select 'public static void main(String args[])' check box  option, deselect 'inherited abstract methods' and click on 'Finish' button


5. Ensure that 'Sum.java' file is displayed in the Eclipse IDE as shown below



6. Write the Java code for calculating the sum of two numbers in the 'main' method as shown below



7. Save the 'Sum.java' file by clicking Eclipse IDE -> 'File' -> 'save' as shown below:



8. Right click on the 'Sum.java' class in the 'Package Explorer' pane and select 'Run As' -> 'Java Application'


9. Ensure that the output of this program is displayed in the Eclipse IDE -> 'Console' as shown below







Please comment below to feedback or ask questions.

How can a class be a template for an object will be explained in the next post.




No comments:

Followers :)

Blog Index