Your first Java program
Steve on Feb 15th 2009
One last thing before actually typing in a program, compiling it and running it. You need a text editor that has features useful for a programmer. Although Windows comes with notepad.exe built in, you really don’t want to be using it to write code. A programmers editor offers special features that help you to write code. These can include colouring different elements of the text in a way that helps understanding, highlighting of useful elements like matching quote symbols or parentheses, automatic help for language keywords, etc.
There are too many programmers editors available to go into a detailed analysis or comparison here. However, a recommendation for a particular editor is also a bit awkward for me to offer! I personally use SlickEdit and have done so for many years, but it’s a very advanced and also very expensive tool (currently $299 for a Windows license). For an advanced programmer already familiar with Integrated Development Environments (IDEs for short), I would recommend Eclipse as probably one of the most used and advanced Java programming environments available. For someone literally starting out programming though, IDEs can be very overwhelming and confusing. Conversely, they often simplify the processes involved in programming (eg. automatic compilation) which beginners need to be exposed to in order to gain a thorough grounding. As I continue these tutorials, there will be natural points where more advanced tools (such as Eclipse) can be introduced, but here at the beginning, I’m going to recommend a free (as in beer and speech) programmers editor called Notepad++.
Installing Notepad++
- First things first, head over to the Notepad++ download page and follow the links to grab the latest version binary install file. Or here’s the direct link for downloading version 5.2 from sourceforge.net.
- Run the installer program and follow the prompts to install. Leave all the options as they are, the installation is quick and painless, and leaves a handy icon on your desktop, which can be put right next to the Java Programs command line icon we created last time.
Coding your first Java program
- When you start Notepad++ for the first time, it loads a file called change.log which tells you all the things that have changed since the last version. Close this by selecting File->Close. This leaves you with an empty file.
- Type in the following Java source code exactly, being careful to match the case of the letters (particularly the H and W or HelloWorld are in upper case). You can indent lines with tabs or spaces, your choice. You can also just select copy to clipboard and paste it into your Notepad++ window.
- Now save the file, making sure it’s called HelloWorld.java into your C:\Java\Programs folder. Do this by selecting File->Save, selecting My Computer and navigating to your C: drive, then into the Java and Programs folders. Then enter HelloWorld.java into the “File name” box and clicking the Save button.
- You’ll notice that after you’ve saved the file, the text in the window changes to include some colour. This is because Notepad++ now recognises your program as a Java program, due to it being a file with .java on the end. All Java source code file must use the .java suffix.
public class HelloWorld
{
public static void main (String [] args)
{
System.out.println ("HelloWorld");
}
}
Compile and run your program
A PC cannot directly execute Java source code, until we get to more advanced topics that is. For now, it suits our purposes to understand that source code can’t be ‘run’ straight away, we need to process it into a form that the computer can understand. The source code needs to be compiled. To do this, you need to use the Java compiler program, javac, from the command line.
- Double click on your “Java Programs” desktop shortcut to bring up the command window, already in the C:\Java\Programs folder, which is handy. If you type dir and press enter, this will see a listing of the files in the folder, which will include our newly created HelloWorld.java source code file.
- Enter javac HelloWorld.java (and press Enter). Nothing will appear to happen unless there are errors in the program.
- Enter dir again and you will see that there is now a file called HelloWorld.class. This file is the compiled code file, it is in an unreadable binary format, but this is how the computer likes it.
- Enter java HelloWorld. This command tells the PC to execute the compiled Java program, and the program prints out the text “Hello World”. If you study the program source carefully, you can see the line that outputs the text onto the screen. See if you can change this text, save the source file, then compile and run the program again.
Here’s what you should see in your command window if everything goes well.
That’s it for now, well done on getting this far. Next time I’ll introduce some errors into the program and we’ll see what happens when things go wrong. If you’ve already had some errors from the javac compiler, carefully check through your source code to see if you can find the problem. Also carefully read the exception message you get when attempting to compile. The Java compiler is actually quite good at recognising problems and will tell you which line of your program is wrong.
Filed in Tutorials | No responses yet





