Introduction to Java
Work through these slides. As you read, think about and answer the questions at the bottom of each page. These will be your first experience with Java, so make sure you follow each step closely.
16. Running the Program
Answer:
Compile it into Java bytecodes, then run the bytecode file with the Java interpreter.
Running the Program
To do all that, find the DOS command prompt window you started up a while back. It should still be positioned in the C:\Temp directory (or other directory of your choice). To check that you put the source file Hello.java in this directory, enter the command dir. You should see the source file, as below (for Windows 8). For Windows 7, the subdirectory will be C:\Users\TEMP (unless you picked a different one.)
Volume in drive C has no label.
Volume Serial Number is 30E8-A977
Directory of C:\Users\YourName\JavaSource
08/14/2014 09:28 PM <DIR> .
08/14/2014 09:28 PM <DIR> ..
08/14/2014 09:28 PM 228 Hello.java
1 File(s) 228 bytes
2 Dir(s) 274,381,590,528 bytes free
To compile the source file (thereby producing a file of bytecodes) enter the command javac Hello.java
.
C:\Users\YourName\JavaSource>javac Hello.java
Notice that this command is javac, java-with-a-c which invokes the Java compiler. If you see the following
C:\Users\YourName\JavaSource>javac Hello.java
The name specified is not recognized as an internal or external command, operable program or batch file.
then the PATH environment variable has not been set correctly. Look at Appendix C for information about this.
As a temporary alternative to a defective PATH, tell the command prompt exactly where to find javac:
Windows 7:
C:\Users\YourName\JavaSource> C:\Program Files\Java\jdk1.8.0_05\bin\javac Hello.java
Windows 8:
C:\Users\YourName\JavaSource> C:\\"Program Files\"\Java\jdk1.8.0_05\bin\javac Hello.java
Adjust the above command to match whatever directory your version of Java has been installed in. You almost certainly will need to change jdk1.8.0_05 to whatever you have installed, perhaps jdk1.7.0_06 or jdk1.8.0_06
Finally, to run the program, enter the command java Hello
.
C:\Users\YourName\JavaSource>java Hello
Hello World!
C:\Users\YourName\JavaSource>
Notice that this command is java, java-without-a-c which invokes the Java interpreter. Again, if PATH is incorrect, you can run java by using the full path name to its location.
Question 16:
After all of this, what did the Java program actually do?