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\JavaYou can tell if you’re at the right directory by looking to the left of the cursor.
Compile Your Program
We’re now ready to compile the program. To do so, enter the command:javac HelloWorld.javaAfter 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
All that’s left to do is run the program. In the terminal window type the command:java HelloWorldWhen 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:
- Write the Java code in a text file
- Save the file
- Compile the source code
- Fix any errors
- Run the program
No comments:
Post a Comment