Thursday, February 4, 2010

java-1.3

Change the Directory

Change the Directory

Next, navigate to where your HelloWorld.java file is saved. As I said earlier, my file has been saved in a directory called "Java" in My Documents. Its location is "C:\Documents and Settings\Paul\My Documents\Java".
To change the directory in the terminal window, type in the command:
cd *directory where HelloWorld.java file is saved*
E.g.,
cd  C:\Documents and Settings\Paul\My Documents\Java
You can tell if you’re at the right directory by looking to the left of the cursor.

Compile Your Program

Compile Your Program

We’re now ready to compile the program. To do so, enter the command:
javac HelloWorld.java
After you hit Enter, the compiler will look at the code contained within the HelloWorld.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.
Hopefully, you should get the same result as me and have no errors. If that’s not the case, go back and check the code you’ve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.
Tip: Once your HelloWorld program has been successfully compiled, you will see a new file in the same directory. It will be called “HelloWorld.class”. This is the compiled version of your program.

Run the Program

Run the Program

All that’s left to do is run the program. In the terminal window type the command:
java HelloWorld
When you hit Enter, the program runs and you will see "Hello World!" written to the terminal window.
Well done. You’ve written your very first Java program!
I’ll admit it’s not the most exciting program ever written, but you now know the steps every Java program has to go through:
  1. Write the Java code in a text file
  2. Save the file
  3. Compile the source code
  4. Fix any errors
  5. Run the program
As the programs get more interesting, these steps will become second nature.

No comments:

Post a Comment