Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 1 – Hello World, and Hello World in 
Eclipse 
Goal: 
The goal of this lab is to teach you how to make and debug a simple Java program. Then you will learn 
how Eclipse, an integrated development environment (IDE), makes a programmer’s job easier. 
 
Part 1 – Hello World with the console 
Start by opening Notepad. Now type the following code into Notepad and save it as 
HelloWorld.java (the name for the file must be the same as the class contained in the file): 
class HelloWorld { 
public static void main(String[] args) { 
String message = "Hello, World!"; 
System.out.println(message); 
} 
} 
Now that you have your code you’ll need to compile it. Open the console by clicking on the Start menu, 
typing cmd.exe, and pressing the enter key. Now navigate to the folder that you just saved your 
HelloWorld.java file in. Type "javac HelloWorld.java" and press enter to run the java compiler on your 
HelloWorld.java file. Assuming you typed the above code correctly the compiler shouldn’t say anything 
and a new file called HelloWorld.class should show up in the working directory. This is the compiled 
version of HelloWorld.java which will execute when the program is run. If you have an error somewhere 
in your code, the compiler will tell you where. Just fix the error and try to compile again. Once you’ve 
successfully compiled your program it’s time to run it. Type "java HelloWorld" into the command line 
and press enter to run your program. You should see the text "Hello, World!" displayed. 
 
This is a really simple program so there’s not much that can go wrong, and anything that does go wrong 
should be easy to find and fix. But in larger programs this isn’t always true. This is where a debugger 
comes into play. A debugger lets you go through a program step by step to find out exactly what’s 
happening. In order to debug your program you’ll need to compile it differently, telling the compiler 
that you intend to debug this program. Simply include the ‘-g’ option when you debug (type "javac -g 
HelloWorld.java"). Now that your code has been compiled with debugging information you can debug 
it. Type "jdb -launch HelloWorld" to start the debugger. The "-launch HelloWorld" option tells the 
debugger to automatically launch the HelloWorld program and wait at the start. From here you can tell 
the debugger to step through your program by typing "step." The step command executes the current 
line of code. In this case, if you type step (go ahead and step if you haven’t already) it will enter the 
program. If you type it again it will execute the line of code that declares and initializes your message. 
Once more will execute the line that actually prints "Hello, World!" and one final step will exit the 
program. 
 
 
 
 
There are a few different ways that you can step through a program. The first, which you just saw, is 
called step into and is done by typing ‘step’. Step into executes a line of code, and if that line of code is 
a method it will try to execute the first line of code in that method. If it can’t (for example, if there’s no 
debugging information associated with the method) then it will just execute the whole method in one 
go. 
 
The next way to step through a program is called step over, which is done by typing ‘next’. Step over 
executes a line of code, and if that line of code is a method it will just execute the whole method instead 
of starting at the first line of code in the method. 
 
The last way to step through a program is called step out, which is done by typing ‘step up’. Step up 
keeps executing lines until the current method ends. So if you type step up while the program was in 
‘main’ it would go until the end of ‘main’ and exit completely. 
 
If you want to just continue running your program you can type ‘cont’. This will run the program until it 
exits. Try debugging your program with these different commands and look at what happens. To see 
other commands type ‘help’ while the debugger is running. 
 
Part 2 – Hello World with Eclipse 
 
Using the command line can be pretty tedious, and it would be nice to have all of the tools available in 
one place. Integrated development environments (IDEs) do just this. Eclipse is a very popular IDE for 
Java development. 
 
Start up Eclipse (search for Eclipse.exe in the Start menu). If this is the first time you’ve used Eclipse 
then you will be asked where you want your workspace to be located. A workspace is just a directory in 
which Eclipse stores the files you create. Eclipse has a default workspace, but you may want to choose a 
location that you only use for this class. Once you’ve chosen your workspace you’ll need to create a 
new project. Up at the top go to File | New | Java Project. Give your project a name, such as 
‘HelloWorldProject’. Accept the remaining default options and press the finish button. On the left you 
should see your project appear in the package explorer. The package explorer gives you a hierarchical 
view of the all the elements in your project. The next thing you’ll need to do is to create a package. A 
package is a way to organize your classes in Java. To create a package click the New Java Package 
button on the toolbar. Give the package a useful name, such as ‘helloWorldPackage’ and click OK. Now 
you can create your class. Click the New Java Class button on the toolbar. Name your class 
HelloWorld. Make sure the modifier is set to public (just below the name) and that the main method 
stub is checked off before you click OK. You should see your new class file generated in the center of 
your screen. You can edit your code files here. 
 
Before you run your program you’ll need to copy and paste the following two lines into your main 
method so that you’ll have some output to look at: 
 
String message = "Hello, World!"; 
System.out.println(message); 
 
Once you’ve done so and saved the file you can build (compile) your project. Normally you will build 
and run your project all at once, but you should know that they’re separate steps. Open the Project 
menu at the top and uncheck ‘Build Automatically’. Again, go to the Project menu and click ‘Build 
Project’. If you have any errors or warnings they will show up under the ‘Problems’ tab near the bottom. 
If not, you’re ready to run the program. Click the Run button on the toolbar. The output will be 
displayed in the ‘Console’ tab near the bottom. Once your program exits you can recheck the ‘Build 
Automatically’ option if you want. This will make your program build when you run it if the most up-
todate version is not already built. 
The last thing you’ll need to do is learn how to use the debugger in Eclipse. But first you’ll need to learn 
about breakpoints. Since Eclipse’s debugger doesn’t automatically pause at the beginning of the 
program, debugging the program will just cause it to run all the way through. Breakpoints allow you to 
pause your program’s execution when it reaches a certain line. For example, if you set a breakpoint on 
line 11 where you print your message then the program will pause just before it executes that line (you 
can enable line numbers by going to Window | Preferences | General | Editors | Text Editors | Show 
line numbers). From there you can step through your program however you want. To set a breakpoint 
just double click on the far left margin of the line where you want to set it. Double clicking an already-set 
breakpoint removes it. 
 
It’s worth noting that the tools the Eclipse debugger has are also available in the JDB command line 
debugger (i.e. you can set breakpoints there, too). Eclipse just has a graphical representation of it to 
make it easier. In fact, you probably can’t do anything with Eclipse’s debugger that can’t be done using 
JDB. 
 
Set a breakpoint on the line where you output your message. To run the program in debug mode click 
the Debug button. Your whole screen should change. This is because Eclipse changed its perspective 
to the debug perspective. A perspective just defines a layout that’s supposed to make it easier to do 
some task in Eclipse. For example the debug perspective gives you a layout that’s supposed to make it 
easier to debug a program. You can switch between perspectives using the buttons in the top right 
corner of your screen. The source button takes you back to the original perspective and the debug 
button takes you to the perspective you’re in now. 
 
Your program should be running and waiting for you to do something. You can use the buttons in the 
debug perspective to step through you program. Everything you’ll need is on the toolbar at the top. Try 
using the different buttons to step through your program like you did before. The line that you’re on is 
highlighted in the text editor and the output is displayed at the bottom of your screen. The current 
values of variables are displayed in the Variables tab, which by default is located in the upper right 
corner of the screen. When you’re done you can change your perspective back to the original (source) 
perspective. Eclipse doesn’t switch back for you when the program exits, so you’ll need to switch back 
manually. 
 
 
 
 
 
 
Questions 
 
1) What is the command to compile a .java file from the command line using the javac compiler? 
What is the command to run a Java program? 
 
2) What does a debugger do and why is it useful? 
 
3) Why is it necessary to compile with the -g option when you intend to debug your code? 
 
4) What are the three different ways to step through a program? What are breakpoints? 
 
5) What are perspectives in Eclipse? 
 
Submission 
 
When you’ve finished the lab, submit your answers in a Word or text document using web-based turnin.   
 
Acknowledgement   
 
Thank you to Tom Whirtner, who designed this lab for CS 220X.