Create your first Java program
1.1. Target of this exercise
The following section describes how to create a minimal Java application using Eclipse. It is tradition in the programming world to create a small program which writes "Hello World" to the console. We will adapt this tradition and will write "Hello Eclipse!" to the console.
1.2. Create project
This book uses the naming convention that the project is named the same as the top-level package in the project.
Select File → New → Java project from the menu. Enter MyProject as the project name. Select the Create separate folders for sources and class files flag.
Press the Finish button to create the project. A new project is created and displayed as a folder. Open the MyProject folder and explore the content of this folder.
1.3. Create package
In the following step you create a new 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 com.example.javaproject you should also use com.example.javaproject as the top-level package name.
To create the firstpackage package, select the src folder , right-click on it and select New → Package.
1.4. Create Java class
Create a Java class. Right-click on your package and select New → Class.
Enter HelloWorld as the class name and select the public static void main (String[] args) checkbox.
Press the Finish button
This creates a new file and opens the Java editor. Change the class based on the following listing.
package firstpackage
public class MyFirstClass {
public static void main(String[] args) {
System.out.println("Hello Eclipse!");
}
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-as → Java application.
Eclipse will run your Java program. You should see the output in the Console view .
0 comments:
Post a Comment