Running Java Programs – Command Line AP Computer Science

A. Preparation

Windows 1. Install the Java Development Kit (JDK). Latest version as of writing is 7u25.

2. Set the PATH variable. a. Navigate to System. Try pressing Windows Key + Pause/Break. b. Open “Advanced system settings”. c. Under the Advanced tab, click Environmental Variables. d. Under System variables, highlight “Path” and press edit. e. At the end of the Path variable, add the full pathname to the directory where you installed the Java JDK: “C:\Program Files\Java\jdk1.7.0_XX\bin”. If you are unsure of where it is, navigate to “C:\Program Files\Java” and verify the folder name.  You may wish to copy the PATH variable into a text file before editing it in case something goes wrong.  Note that each path is separated by a semicolon. f. For more information on setting PATH: http://java.com/en/download/help/path.xml.

3. Verify that you have successfully configured the PATH file. a. Open a new command line window. Press Windows Key + R, typing in cmd, and enter.  Alternatively, press Windows Key, search for cmd, and select it. b. Type in “javac -version” and enter. If you have configured PATH correctly, it should say something along the lines of “javac version 1.7.0_XX”.  If the command line window says “javac is not recognized…”, you have not configured PATH correctly. Recheck step 2 above.

Mac 1. Depending on your version of OSX, the Java Development Kit (JDK) may already have been installed. To check, follow step 3. 2. If you do not have the JDK installed, or wish to update, do so now. Check Software Updates, or go to the Oracle website to download. Latest version as of writing is 7u25. 3. Verify that the JDK is installed. a. Open a new Terminal window. All macs should have it under the Applications folder. b. Type in “javac -version” and enter. If the JDK is installed, it should say something along the lines of “javac version 1.7.0_XX”.  If the Terminal window says “javac: command not found”, the JDK is not installed. Recheck step 2 above. Running programs

1. The commands used:  cd – Change directory. Example usage: cd FolderName, cd .. (to go to a folder)  javac – Java compiler. Example usage: javac HelloWorld.java  java – Java runtime. Example usage: java HelloWorld

2. Basic gist: Use javac to compile your source code (files with extension .java) into Java Virtual Machine code (files with extension .class). Use java to run these class files.

B. Sample 1. The source code: HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.print(“Hello World!”); }

2. Open a command window (if you are using Windows) or Terminal window (Mac). For this sample, I will be using the Windows command processor cmd. 3. Navigate to the location of the source file “HelloWorld.java” by using cd. 4. Compile the file by using javac. 5. Run the program by using java. 6. Result:

C. Tips and tricks Knowing command line shortcuts is extremely helpful. Here are a few that could prove useful.  Ctrl + c (cmd and Terminal) – This is not copy, but clear the current command. Also can be used to stop programs that are executing.  dir (cmd) or ls (Terminal) – Lists all files in the current directory.  Tab – Can be used to fill partially written filenames. Typing in “javac He” and pressing tab will iterate through all files in the current directory that start with “He”.  Up/Down – Cycle through previously used commands.  For more Mac tips: http://osxdaily.com/2007/02/07/basic-command-line-utilities-tips-commands/; http://www.youtube.com/watch?v=ftJoIN_OADc