[ [[tutorial-en-001 | Intro]] | [[tutorial-en-002 | Begin]] |
[[tutorial-en-003 | if/else]] | [[tutorial-en-004 | Loops]] |
[[tutorial-en-005 | Arrays]] | [[tutorial-en-006 | Graphics]] |
[[tutorial-en-007 | Animation]] | [[tutorial-en-008 | Mouse]] |
[[tutorial-en-009 | Game]] | [[tutorial-en-010 | Real]] |
[[tutorial-en-011 | Methods]] | [[tutorial-en-012 | Class]] |
[[tutorial-en-013 | Class 2]] | [[tutorial-en-014 | Applet]] |
[[tutorial-en-015 | MouseClick]] | [[tutorial-en-016 | Thread]] |
[[tutorial-en-017 | Button]] ]
----
====== Java - Using the Command Prompt ======
Now we are going to start using Real Java. JUDO uses it but it is like
an interface between us and Java. Firstly find out where your Java is
situated on your hard drive. Do a search for "javac.exe".
Find the directory path for it. Write it down. Mine is "E:\jdk\bin\".
Open your Text Editor (NotePad, Crimson Editor) and write your first
real java program. It has to be hello world of course. Write this in
your text editor:
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Save it as Hello.java in a directory such as "d:\javatute\".
Open up your Dos prompt. Go to Start / Programs / MSDOS Prompt. You will get
a black window with a prompt that says somethin like c:\windows>
Write "doskey" after the prompt. That will allow you to get old commands
back using the up and down keys.
Type in "D:\ " To take you back to the root directory of Hello.java 's file.
Type in "cd javatute" to take you to the directory where we saved
Hello.java to.
If you have saved your file somewhere else then go to that directory using
your directory names.
===== Compiling your Java Program =====
When you get to your directory, your command prompt will look like:
d:\javatute>
Then type in the directory of your javac file, javac, and Hello.java
For me it will be: "e:\jdk\bin\javac Hello.java"
And press return. You should get nothing. It will just return you
to your command prompt you had before. If you have done anything
wrong it will tell you the error you made and the
line that you made it on.
Press the up arrow key and you will get "e:\jdk\bin\javac Hello.java"
back again. Delete back to javac
and replace the line with: "e:\jdk\bin\java Hello".
You should get "Hello World" printed on the next line and be returned
to the command prompt.
What have you just done? You have compiled and run your first java program
all by yourself. javac is the command that compiles your text file
Hello.java into machine code that the computer can understand.
Computers can't understand English, they only understand 0's and 1's.
They are very dumb. A compiler allows us to write Java code and it will
compile it for us into a language that the computer will understand.
Imagine how hard it would be typing in 0's and 1's!
It would drive you round the twist! It might look something like this.
010100101000101010 01010100101010 101010101010 101000001 01111000110000111
0001010010101010010 01010101010100000101010010111010101000 0001000010001000011
01010101010010101 01010101010000010
===== Run your Java Program =====
Sick of that yet? Once your java program is compiled, you need to RUN it.
java is the command that runs your program. All you need to do to run
a command-line program is type in "java" followed by the program name.
In this case "Hello".
You now know how to compile and run a java program.
Lets analyse our code for "Hello.java".
First line: "public class Hello" - this declares our class name.
Or what our program is called. The class name must be the same as
the name it is saved as. So if I have a class called "Brick"
then i must save it as "Brick.java". If not you will get an error message.
"public static void main(String[] args){" is the method that runs
an application. You need it in every java program (except for java
web applets). Inside the main method is where you write
the code for the program to run.
"System.out.println("hello world");" - is simply the longer version
of the JUDO "printLine" command.
You already know heaps of java from our earlier tutorials using JUDO.
The syntax is the same for variables such as int, double, String.
We will be using arrays, conditional loops such as while, for.
And in the lessons on Graphics we have laid the foundations of work
that we will do later on animation and games. You already know heaps
of java if you have done the previous lessons on JUDO. You will
just be accessing it directly instead of through an interface or GUI.
Here is a program from Lesson 4 dealing with loops. Lets reuse it as
a Java program. Write this in your text editor:
public class Counter
{
public static void main(String[] args) {
// declare our variable
int count = 0;
// loop 8 times
while (count < 8)
{
System.out.println("Count is equal to ", count);
}
}
}
Save it as "Counter.java". Compile it using the command
"[mydirectory]\javac Counter.java" Where [mydirectory] is where javac is.
If there are no errors, run the program using the command
"[directory]\java Counter".
What did you get? should be the same as lesson 4.
=== Java Exercise ===
Go back to the previous lessons and redo them using the javac compiler
and proper java syntax. Mostly all you will need to do is put in a line
at the top, declaring your class name and change the "void main" line
to "public static void main(String[] args)".
Also you will need to change "printLine" to "System.out.println".
Don't worry about the ones with readInt(). We will go into that later...