Eclipse: Tutorial Penggunaan

From OnnoWiki
Revision as of 09:50, 3 September 2011 by Onnowpurbo (talk | contribs)
Jump to navigation Jump to search

Tutorial ini menjelaskan penggunaan Eclipse sebagai IDE Java. Tutorial ini menjelaskan cara instalasi Eclipse, pembuatan program Java dan tip penggunaan Eclipse. Tutorial ini berbasis pada Eclipse 3.7 (Indigo)

Eclipse Secara Umum

Umumnya kita mengetahui Eclipse sebagai Integrated Development Environment (IDE) untuk Java. Eclipse dibuat oleh komunitas Open Source dan digunakan di beberapa hal, seperti, development environment untuk Java atau Android atau platform untuk aplikasi RCP Eclipse.

Penggunaan Eclipse sebagai Java Development Environment akan di terangkan di tutorial ini.


Memulai

Instalasi Eclipse

Instalasi Eclipse sangat mudah

apt-get install eclipse

Lumayan besar hampir 400Mbyte :) ...

Start Eclipse

Untuk menjalankan Eclipse ketik / double klik pada

eclipse

di shell. System akan menanyakan dimana lokasi workspace. Workspace adalah tempat dimana kita dapat menyimpan project Java. Pilih directory kosong dan tekan OK.

Eclipse akan jalan dan akan memperlihatkan Welcome page. Tutup welcome page dengan menekan "X" sebelah "Welcome".


3. Eclipse UI Overview

Eclipse provides perspectives, views and editors. Views and editors are grouped into perspectives. All projects are located in a workspace. 3.1. Workspace

The workspace is the physical location (file path) you are working in. You can choose the workspace during startup of eclipse or via the menu (File-> Switch Workspace-> Others). All your projects, sources files, images and other artifacts will be stored and saved in your workspace.

You can predefine the workspace via the startup parameter -data path_to_workspace, e.g. "c:\eclipse.exe -data "c:\temp" Please note that you have to put the path name into brackets. To see the current workspace directory in the title of Eclipse use -showLocation as additional parameter. 3.2. Perspective

A perspective is a visual container for a set of views and editors. You can change the layout within a perspective (close / open views, editors, change the size, change the position, etc.). Eclipse allow you to switch to another perspective via the menu Window->Open Perspective -> Other. For Java development you usually use the "Java Perspective".

Tip A common problem is that you closed a view and don't know how to re-open this view. You can reset a perpective it to it original state via the menu "Window" -> "Reset Perspective".

3.3. Views and Editors

A view is typically used to navigate a hierarchy of information or to open an editor. Changes in a view are directly applied to the underlying data structure. Editors are used to modify elements. Editors can have code completion, undo / redo, etc. To apply the changes in an editor to the underlying resources, e.g. Java source file, you usually have to save. 4. Create your first Java program

The following will describe how to create a minimal Java program using Eclipse. It will be the classical "Hello World" program. Our program will write "Hello Eclipse!" to the console. 4.1. Create project

Select from the menu File -> New-> Java project. Maintain "de.vogella.eclipse.ide.first" as the project name. Select "Create separate source and output folders".

Press finish to create the project. A new project is created and displayed as a folder. Open the folder "de.vogella.eclipse.ide.first" 4.2. Create package

Create now a package. A good convention is to use the same name for the top package as the project. Create therefore the package "de.vogella.eclipse.ide.first".

Select the folder src, right mouse click on it and select New -> Package.

4.3. Create Java class

Right click on your package and select New -> Class

Create MyFirstClass, select the flag "public static void main (String[] args)"

Maintain the following code.


package de.vogella.eclipse.ide.first;

public class MyFirstClass {

public static void main(String[] args) { System.out.println("Hello Eclipse!"); }

}


4.4. Run your project in Eclipse

Now run your code. Right click on your Java class and select Run-as-> Java application

Finished! You should see the output in the console.

4.5. Run your Java program outside Eclipse (create jar file)

To run your Java program outside of Eclipse you need to export it as a jar file. Select your project, right click on it and select "Export".

Select JAR file, select next. Select your project and maintain the export destination and a name for the jar file. I named it "myprogram.jar".

Press finish. This will create a jar file in your select output directory. 4.6. Run your program outside Eclipse

Open a command shell, e.g. under Microsoft Windows select Start -> Run and type in cmd. This should open a consle.

Switch to your output directory, e.g. by typing cd path, e.g. if you jar is located in "c:\temp" type "cd c