Now that you've created your
project, you now want to create a new Java file and add it into your project.
Your Java file (files with a
.java extension) is a plaintext file in
which you will write your code. Once you've created your project, you will see
it listed in the "Package explorer" pane in the left of the screen. (If you
don't see that, switch to either Dr. Java or Java perspective.) To create a new
Java file, right click on your project and select New -> Class. You'll see a
window that looks like this:
In the "Name: " section provide the
name for the file you want to create. In addition, in the section "Which method
stubs would you like to create?" select "public static void main(String[] args)."
Once you have completed your selection click on "Finish." This is the window you
will see:
Click on the area where "public
class HelloWorld" apperas. Then scroll down the pane. You will see that a class
HelloWorld with a main method has been created for you.
Saving, compiling, and running Java code
Ready to write some code?
Paste
this line into your public static void main(String[] args) method, in
between the { and the }:
System.out.println("Hello
world!");
The whole method should look like
this:
public static void main(String[] args) {
System.out.println("Hello world!");
}
In order to actually run this
code, the code needs to be compiled, i.e., translated into a language the
computer can understand. There are different approaches we can follow one of
them is by saving the file. In Eclipse saving a file compiles the file for us.
To save a file select File->Save.
Now that your code has been
compiled, you can run your program by selecting Run -> Run As -> Java
Application. You will be able to see the output of your program by selecting the
Console tab you will find near the Dr. Java interactions pane.
Resetting your perspective
You may notice that certain
components of the various perspectives, for example the Console window, or the
Package Explorer, can be closed via the X widget in their upper right corners.
What happens if you accidently close the console, for example? How will you see
what's going on? If that happens, you'll want to reset your perspective and get
back any windows you may have accidently closed. To do this, all you need to do
is select Window -> Reset Perspective and click "OK" when prompted for
confirmation. Your perspective will be restored to its original state with all
of its windows intact.
Quitting Eclipse
You can exit eclipse by using any
of the following alternatives:
- Hit the X in the upper right corner
- Select File -> Exit
- Pressing ALT-F4