Eclipse IDE is widely used in working environments. Most professional prefer to use Eclipse as it is considered to be industry standard IDE. This tutorial will help you in installing and configuring Eclipse along with writing and compiling the code.
In general Eclipse is used in working environments where you have lot of packages and so many files. In each project you can create only one main file and it always cumbersome to create a new project whenever you want to write a new main program. Our primary purpose is to create a new file with main method to verify our understanding of SCJP concepts. In few moments you will see how to do that with out creating a new project [ Hint : We use Java Interfaces ]
1) Download Eclipse IDE from the following link. http://www.eclipse.org/downloads/ . Select Eclipse Classic 3.6.1 from the list of available Eclipse Downloads.
2) Unzip the downloaded file and open eclipse.exe.
3) Eclipse will store all your files in a workspace. You can create multiple workspaces, but as of now we are creating a new workspace folder and going to store all our files in specific location. I selected C:\workspace but it is up to you to chose the workspace folder.
4 ) You will encounter the following screen when you open Eclipse for the first time. Select workbench as marked in blue to continue.
5) Select File –> New –> Java Project. All Java files will reside in a project. So the first step is to create a new project.
6) Give a meaningful name to the new Java project. I gave SCJP you can choose what ever you want. You can see that all the files created in this project will be stored insid c:\workspace\SCJP. In that c:\workspace is my workspace folder and SCJP is the name of the project.
7) To create a new Java file or interface right click on the src folder and select class or interface.
8) In this tutorial I will create a new interface with name MainInterface by doing a right click on src and selecting Interface option. Click finish to create a new interface.
9) Eclipse will create the interface for with name MainInterface. Now your task is to define the methods. I created a method called mainMethod() , to understand what this method does read the comments mentioned above the method.
10) Now create a new Java class with name FirstExample and implement MainInterface.To implement MainInterface you have to override mainMethod(). Just assume that this is your main method and write what ever you want.
11) Now we will write the main class for this project. This main class will invoke the mainMethod() of MainInterface object.
12) Now if you want to write another class create a new class with name SecondExample and implement MainInterface. Write your main() method logic inside mainMethod() and do the following modification in the original SCJPMainClass to make it work.
13) To execute SecondExample.java you have to make small changes in your SCJPMainClass by creating an object of SecondExample and calling its mainMethod().
Since Eclipse allows only one main method/class to be written per each project, this approach will eliminate the pain of creating new projects in order to write a new main class.
code :
public interface MainInterface {// This method will be overriden by all our scjp// sample practice files and actual main class will// call this mainmethod.public void mainMethod();}
public class SCJPMainClass {// This is the actual main class and it invokes// MainInterface methodpublic static void main(String[] args){MainInterface mi = new FirstExample();mi.mainMethod();}}
public class FirstExample implements MainInterface{@Overridepublic void mainMethod() {System.out.println("First program.");}}
No comments:
Post a Comment