In section 1.1 of Computer Science: An Interdisciplinary Approach, Robert Sedgewick and Kevin Wayne break the process of developing a Java program down into the following high-level steps:
- Create a program by entering/saving it in a file with a
.java
filename extension. - Assuming the file containing the program is named, say,
Foobar.java
, compile the program by typingjavac Foobar.java
- Execute/run the program by typing
java Foobar
Although this is a nice summary of the process of creating, compiling and running a Java program, it might not answer all of your questions. For example, you might be wondering, what is a terminal program and where can I find one? Maybe when you type javac
on your computer at a command prompt (presuming you got that far), your computer says it doesn’t recognize the command, even though a version of the JDK is installed on the computer. Now what are you supposed to do?
Answers to some of these questions might be provided on the booksite that accompanies Sedgewick and Wayne’s book. Somewhat general instructions for developing your first program on various operating systems are provided on this page. If that information helps you, then great! Otherwise, the information that follows in this document might be helpful in some cases. In particular, this document is aimed at students enrolled in the 2017–2018 offering of AP Computer Science A taught at Valley Catholic High School.
Windows PowerShell
This is what “plain” PowerShell looks like. |
This cropped image shows what PowerShell ISE looks like. |
javac
at a command prompt and press enter you see something that looks like this:
PS C:\Users\jspurgeon> javacjavac : The term 'javac' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + javac + ~~~~~ + CategoryInfo : ObjectNotFound: (javac:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundExceptionPS C:\Users\jspurgeon>
In that case, you can follow the instructions in this section to create an PowerShell alias called ‘javac’ (or any other name, for that matter) that references the javac.exe
program on your computer, assuming it exists in a location that is identical to or sufficiently analogous to the installation implied by the following instructions.
No matter what, you can always create PowerShell aliases to refer to things.
But if the computer you are using to compile and execute Java programs has been configured such that it knows how to find the javac.exe
and java.exe
programs when you type javac
and java
respectively, then creating PowerShell aliases is not necessary.
set-alias javac
Ultimately, the command we want to execute is a command like this one:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\bin\javac.exe'
If and only if your JDK is installed in exactly the same location as the path shown above, then you can simply copy the set-alias
command above, paste it at a PowerShell command prompt, and execute the command by pressing Enter
, assuming you didn't copy an invisible return character before pasting.
However, there is a very good chance that many computers right now and especially in the future will have a JDK installed in a different location, since new versions of JDK bundles are installed in locations that correspond to the version number that identifies the JDK software package.
Therefore, the following steps might be a more reliable way to get your aliases configured correctly every time.
First, type the beginning of the set-alias
command as follows:
PS C:\Users\jspurgeon> set-alias javac c:\pr
Now press the Tab
key, and you should see that the PowerShell command interpreter has automatically appended a few more of the characters that you need in your particular set-alias
command.
For example:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\'
Next, type the letter ‘j’ (or possibly the characters ‘\j’ if a backslash was not appended when you pressed Tab
). The letter ‘j’ is the first letter in the name of the folder called ‘Java’ that exists in the folder C:\Program Files\
on the computer I'm using.
Windows PowerShell PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\j
Then press the Tab
key again, and hopefully you will see something that looks like this:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\'
If you made it as far as the previous step, but your PowerShell command interpreter doesn’t automatically append the characters shown in the command above,
then perhaps a JDK package is not installed on your computer yet,
or perhaps it is installed somewhere other than in the folder C:\Program Files\
,
or perhaps you typed something incorrectly.
If you are having trouble, first double check to make sure you followed these instructions carefully; then try to get some help from a personal friend or a friendly person.
If you are still sailing smoothly, then type another ‘j’:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\j
Press Tab
again:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\'
Assess the situation again. Sometimes the Java
directory exists, but there is no jdk subdirectory containing JDK files inside it.
If a JDK package is installed on your computer, this is the point at which you are most likely to notice that your set-alias
command starts to look a little different from the command shown in this example, because your JDK version might be different.
If you are still sailing smoothly, then type the letter ‘b’:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\b
Press Tab
:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\bin\'
Type javac
:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\bin\javac'
Press Tab
:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\bin\javac.exe'
If your set-alias
command looks very similar to the one shown above, you’re probably golden.
Press Enter
:
PS C:\Users\jspurgeon> set-alias javac 'C:\Program Files\Java\jdk1.8.0_111\bin\javac.exe' PS C:\Users\jspurgeon>
Now you can execute the javac.exe
program by simply typing java
at the command prompt, regardless of which directory you are in.
For example:
PS C:\Users\jspurgeon> javac Usage: javac
set-alias java
Assuming you successfully created a PowerShell alias called javac
, you might need to do almost exactly the same thing to create an alias called, for example, java
that refers to the Java interpreter/class loader java.exe
.
(It is possible for a computer to be configured such that a javac
alias is required but an alias referring to the java.exe
program is not needed.)
You may either create a java
alias from scratch, so to speak, using a procedure analogous to the one described above.
Or, if you just created your javac alias, you can simply press the up arrow key on your keyboard;
this should place the previously executed command on the command-line, ready to be executed;
at that point, before pressing Enter
(which would simply execute the same set-alias command again), you can modify the command slightly so that it looks like this:
PS C:\Users\jspurgeon> set-alias java 'C:\Program Files\Java\jdk1.8.0_111\bin\java.exe'
Then press Enter
.
Note that the two instances of the string “javac” in the first set-alias
command were changed to the string “java” by deleting two instances of the letter ‘c’.
Use the arrow keys and the backspace or delete keys on your keyboard to accomplish this remarkable feat.
Now, when you type java
and press Enter
, you should see something like this:
PS C:\Users\jspurgeon> java Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include:
cd
The next step in the process of preparing to compile and run a Java program is to use the PowerShell cd
(change directory) command to go to the directory in which your Java program file or files have been saved. For example:
PS C:\Users\jspurgeon> cd C:\Users\jspurgeon\MyPrograms\ PS C:\Users\jspurgeon\MyPrograms>
Your Java programs could be stored in any one of many places on one of your computer’s local drives (e.g. the C:\
drive) or on a network drive.
You need to become familiar and comfortable with procedures for locating your files and determining the full pathnames of those files so that you can can pass the full (or possibly a partial) pathname as the argument to the cd
command as shown above.
ls or dir
You can use either the PowerShell ls
(list) or dir
(directory) commands to display the contents of a directory.
For example:
PS C:\Users\jspurgeon\MyPrograms> ls Directory: C:\Users\jspurgeon\MyPrograms Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/13/2017 12:34 PM 0 HelloWorld.java PS C:\Users\jspurgeon\MyPrograms> dir Directory: C:\Users\jspurgeon\MyPrograms Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/13/2017 12:34 PM 0 HelloWorld.java PS C:\Users\jspurgeon\MyPrograms>
Finally!
Now you can begin trying to program in Java. For example:
PS C:\Users\jspurgeon\MyPrograms> javac HelloWorld.java PS C:\Users\jspurgeon\MyPrograms> ls Directory: C:\Users\jspurgeon\MyPrograms Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/13/2017 12:34 PM 0 HelloWorld.java PS C:\Users\jspurgeon\MyPrograms>
Doh! It would help to have some text in the HelloWorld.java file.
For example:
public class Foobar { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Try again:
PS C:\Users\jspurgeon\MyPrograms> dir Directory: C:\Users\jspurgeon\MyPrograms Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/13/2017 12:50 PM 115 HelloWorld.java PS C:\Users\jspurgeon\MyPrograms> javac HelloWorld.java HelloWorld.java:1: error: class Foobar is public, should be declared in a file named Foobar.java public class Foobar { ^ 1 error PS C:\Users\jspurgeon\MyPrograms>
Oops! I did it again. Programming is such a persnickety pastime. Third time’s a charm...
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Now:
PS C:\Users\jspurgeon\MyPrograms> javac HelloWorld.java PS C:\Users\jspurgeon\MyPrograms> ls Directory: C:\Users\jspurgeon\MyPrograms Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 9/13/2017 12:54 PM 427 HelloWorld.class -a---- 9/13/2017 12:54 PM 119 HelloWorld.java PS C:\Users\jspurgeon\MyPrograms>
Success! (Compile-time success, at least.) Now for the real test...
PS C:\Users\jspurgeon\MyPrograms> java HelloWorld Hello, World! PS C:\Users\jspurgeon\MyPrograms>
Run-time success too!!
Oh where oh where has my little prog gone?
Creating a text file with Notepad and saving it somewhere is pretty straight forward. Unfortunately, figuring out exactly where that file has been stored seems to get harder and harder with every new version of Windows.
If you are using Windows 10 and are trying to compile and execute Java programs using a JDK and a command prompt, you might be having trouble finding your .java
source code files.
If so, here are a couple of techniques that might help.
Use the Properties Icon
One way to determine the location of a file in terms of its full pathname is to use the Properties icon in Windows File Explorer. On my computer, the icon is located in the upper left corner of the File Explorer window.
When you click on the Properties icon, the Programs Properties
(sic) window appears.
Display Path in Title Bar
In the screenshots above you might have noticed that the pathname shown in the Programs Properties
window is also shown in the title bar of File Explorer.
If the pathnames of your folders and files are not shown there, you can change that as follows
—
assuming the software you are using is similar to mine, of course.
Locate the View
menu.
Click on the view menu and location the Options
icon on the far right hand side of the toolbar.
Click on the Options
icon.
Then click on Change folder and search options
. (Above.)
The Folder Options
window will appear. (Below.)
Select the View
tab on that window.
Check Display the full path in the title bar
.
Press Apply .
|
Voila! |