If you were a bit perplexed trying to understand 11101010111100000101010111 then you know exactly how a computer feels when it tries to understand the program you just typed in:
class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
|
It's written in a human-friendly way but not in a very
computer-friendly way. So we need to translate it into something a lot
more like the string of numbers above. The technical word for this is compiling, using something called a compiler.
So then, let's kick ass! Go to the other window. Now leap out of it
and fall to your death. Good, that's got rid of all the really thick
people. So, for the rest of you, go to the window on your screen marked
"Terminal". Type in ls.
[nurmes]btg: ls
Desktop Hello.java
[nurmes]btg:
|
The command ls is used to list all the files in your directory. You can see Hello.java is there (if it's not then make sure you saved it). Let's look at it. Type more Hello.java.
[nurmes]btg: more Hello.java
class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
[nurmes]btg:
|
Just as I expected. Now time to compile the program (it's that word again) - in other words convert it into zeros and ones. Type javac Hello.java.
[nurmes]btg: javac Hello.java
[nurmes]btg:
|
|
[nurmes]btg: javac Hello.java
Hello.java:3: ';' expected.
System.out.println("Hello, world!")
^
1 error
[nurmes]btg:
|
If you've not made any mistakes in the program then your screen should
look like the one on the left, in other words nothing is printed out.
However please read the text on the right as you will make mistakes
sometime. Then go on to the next paragraph.
|
|
Unfortunately if it looks like the screen on the right then something
is up. If you see a message like this then you've made a typing error
somewhere. I'll explain what this message means for later reference.
The first part (Hello.java:3: ';' expected. ) gives the filename, line number and what it thinks is wrong. Line numbers are displayed at the bottom in kwrite
so this is useful with bigger programs. The next part lists the line of
the program which had the error, and just under that there is a little
arrow (^ ). The error tells us that it was expecting
a semicolon, and the arrow is pointing just after the end of the line.
So it should be clear exactly what went wrong and where. Sometimes it's
not so obvious, but don't worry about that yet. Fix any mistakes you
have by looking at the original program (in green at the top of this
page), then save your work and try again until you get no errors. |
So if you've got here you should have successfully compiled the Java, so that after you typed javac Hello.java
, nothing was printed on the screen. Now list all the files again like you did before (remember, type ls
).
[nurmes]btg: ls
Desktop Hello.class Hello.java
[nurmes]btg:
|
Notice that another file has now appeared, called Hello.class
. Take a look at it (type cat Hello.class).
[nurmes]btg: cat Hello.class
Êþº¾-
ConstantValueg/String;)V([Ljava/lang/String;)VCode
Hello, world!lo
Hello.javaLineNumberTableLjava/io/PrintStream;LocalVariables
SourceFilejava/io/PrintStreamjava/lang/Objectjava/lang/Systemmainoutprintln % ²±
*·±
[nurmes]btg:
|
If you don't understand this gobbledygook, good, that makes two of us. This is a computer-friendly version of Hello.java
. But until we have this, we can't do anything. Now then, let's run the program. Type java Hello.
[nurmes]btg: java Hello
Hello, world!
[nurmes]btg:
|
Wow, spectucular, huh? Multi-media-tastic, baby! Anyway, that was it. When you typed java Hello, the computer looked at the file Hello.class and then did what it said, ie print Hello, World! to the screen.
So that concludes this lesson. Phew. From now on
we'll be going a lot faster, and I'll assume you know how to do this
stuff, so if you can't do it then come back and do this lesson again.
Just one bit of revision:
If we have a program which starts class Hello {
then it must be saved as Hello.java. To compile it we type javac Hello.java and, once we have successfully compiled it, we type java Hello to run it. A common mistake is to type some mix-up version of these two, so remember to put in the .java
when using javac and leave it out when using java.
If you're feeling adventurous, it's worth fiddling
around with the program just to get the hang of it. Try changing the
message so it prints "Hello, Mum!". Try adding another statement
like the first one, just below it, to print another message. If you
can't work out what the hell I'm on about then change your program to
look like this, compile and run it and see what it does.
class Hello {
public static void main(String[] args) {
System.out.println("Hello, Mum!");
System.out.println("Hello, Dad!");
System.out.println("Hello, cat!");
}
}
|
Tell me more, tell me more...
(Grease anyone?)