Instructions First, download the file http://www.cs.mcgill.ca/~cs202/2012-09/web/lectures/dan/unit0/helloworld.java from the course webpage. You can view this file in a program such as notepad (windows), textedit (macintosh) or emacs (linux). Depending on how your computer is set up, you may need to first open notepad/textedit and THEN click file-->open as opposed to double clicking. When you write a program from scratch, you need to be careful of a few things: 1)On windows, when you save, make sure to save as HelloWorld.java and not HelloWorld.java.txt (select all files when you choose where to save and put HelloWorld.java in quotations) 2)On Mac, make sure when you click format that there is no button make plain text (If there is, then click the button to make it plain text) 3)Always make sure the file name matches exactly (including upper/lowercase the first line public class HelloWorld 4)Note the folder where you saved this file. This will be used later on. On Mac, it is useful to put it within a subfolder of Documents COMP-202 - Course Details 1
Mac Instructions Most likely, on a Macintosh computer, Java is already installed. All you need to do is open up a terminal prompt to be able to run it. Click applications--->utilities and look for a program called terminal You'll probably want to drag it to the bottom of the screen as you'll use it frequently Now open this program and a black box will appear. Type pwd and then enter You'll see a folder on your computer. It will most likely be something like: /Users/daniel The first thing you need to do is change folders to where you saved HelloWorld.java If you saved it in a folder such as Documents/ something or other you can type cd Documents/comp202/exampleprogram (if you have spaces in your folders then you need to do cd Documents\comp 202\example program with quotes around the folder path) Now skip ahead a few slides to the merged instructions COMP-202 - Course Details 2
1)If you don't have Java development kit installed, install it. Make sure to get version 6 at least. http://www.oracle.com/technetwork/java/javase/documentation/install-windows-152927.html (instructions) http://www.oracle.com/technetwork/java/javase/downloads/jdk6u35-downloads-1836443.html (link to download the update is fine) http://download.cnet.com/java-se-6-0-jdk-6/3000-2212_4-10857945.html (also should work) 2)Install Java by double clicking the exe and following the instructions COMP-202 - Course Details 3
3)The next step is to locate the folder that Java is installed in. It should be something like: C:\Program Files\java\jdk1.6\ Now within this folder, look for a folder called bin and inside this an exe file called javac The exe javac is what you will use to convert your source code written in the programming language Java, to something the computer can understand better. However, you will not run the program javac in the usual way of double clicking it. COMP-202 - Course Details 4
3)The next step is to locate the folder that Java is installed in. It should be something like: C:\Program Files\java\jdk1.6\ Now within this folder, look for a folder called bin and inside this an exe file called javac. Select this whole path (including /bin but not javac) and copy it using ctrl+c if necessary) It is important you copy it EXACTLY The exe javac is what you will use to convert your source code written in the programming language Java, to something the computer can understand better. However, you will not run the program javac in the usual way of double clicking it. COMP-202 - Course Details 5
4)Tell your computer where it can find javac and java by adding this path you copied to your environment variable called PATH http://docs.oracle.com/javase/tutorial/essential/environment/paths.html -Follow the instructions at the above link. You'll want to make sure that the PATH variable includes something like the following: C:\Java\jdk1.6.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System 32\Wbem The bold is the path you copied in the previous step. If it is not there, then you need to add it by adding a ; at the end of list, then put the path there. Make sure this is EXACTLY the path where the file javac and java can be found. Note that this step you will only need to do one time COMP-202 - Course Details 6
5)Type start-->run. Then type cmd. A black box should appear. Type javac and then enter COMP-202 - Course Details 7
6)If you get a message such as command not found it means you did not properly install Java or set up the path in the prior steps. If you get a more verbose message starting with no input files then congratulations, you have properly installed everything and we can now go on to run a Java program. If you can't fix this but you are able to find a file javac on your computer, another option, in the short run is to type: C:\program files\jdk1.6.0\bin\javac HelloWorld.java (or whatever folder your file javac is in). COMP-202 - Course Details 8
7)First, notice at the prompt that a folder is displayed. In the screen shot above it is C:\Documents and Settings\Administrator You need to make this folder match the folder that you saved your.java file in. To do this there are a few ways. The simplest is to type: cd the full path with the folder (e.g. cd C:\comp202\testprograms ) Also can do it one directory at a time: cd foo -->changes into a folder foo that is inside current folder cd.. ---> changes to parent folder cd C:\ --> changes to root folder D: ----> changes to D drive COMP-202 - Course Details 9
Merged instructions : Compiling your code Once you have changed folders and have everything set up, type: javac HelloWorld.java then hit enter. If this works successfully, you will see nothing at this point. If you then look inside the folder by typing dir on windows or ls on Mac (or view in the normal way to view folders with graphics), you'll see a file HelloWorld.class now This file is the file that was created by the program javac. It is your source code (human text) converted to a format the computer can understand. This is called compiling your code If any grammatical errors occurred in your program, you will get errors at this point and no new.class file will be created. (Experiment with this by, for example, deleting one of the { from the HelloWorld.java file and see what happens) COMP-202 - Course Details 10
Merged instructions : Running your program Once you create the.class file, you can run your program by typing: java HelloWorld Be careful because it is case sensitive. Typing java helloworld will give an error. If everything worked, the words Hello world! should print to the screen. Congratulations! In Java you will always run a.class file, hence it is not necessary (or allowed) to type java HelloWorld.class as this means something entirely different. COMP-202 - Course Details 11