Building Your First Java Program: Starting From Scratch
Welcome to CS 1322! If this is your first time using a corporate-grade
programming language, then you will probably find this tutorial quite useful! The point
of this guide is to familiarize you with the basic workings of Java enough to allow you
to install and configure the Java Virtual Machine, set up any path and classpath variables, and plug
an Integrated Development Environment (IDE) into the works to write the actual Java code. Hopefully,
this will give you enough knowledge and elbow room to experiment and learn more about Java
on your own. Good luck!
|
Java Installation Overview |
(Win)
(Mac) |
|
Configuring the Java Virtual Machine |
(Win)
(Mac) |
|
Overview and Installation of IDEs |
(Win)
(Mac) |
|
Writing a Java Class |
(Win)
(Mac) |
|
Conclusion |
(Win / Mac) |
Professor Orso's slides to
further help with this process

Java Installation Overview - Windows
This part will cover installation of the newest version of Java, 5.0,
on your computer. First, however, you must check to be sure that you have no previous
version of Java installed and, if you do, you must uninstall it before continuing. So
first we will check for previous installations. (Note that the previous version of Java was 1.4.x, and this one would equivalently be 1.5.0, but Sun is simply calling it Java 5.0.)
Let's begin by opening up your Control Panel. Double click on
"My Computer", and from the menu along the left panel of the window, select "Control
Panel."
Important! Once the Control Panel comes up, check the left panel
again. If there is an option there called "Switch to Classic View," select it now.
Otherwise, if it says "Switch to Category View," leave it alone.
Now, select the "Add or Remove Programs" icon from the list - it should
be one of the first couple ones across the top. From the list of programs that then
pop up, scroll downward and look for any program(s) with the name "J2SE" in it. If you
find one or more, note the version number. If the version number is different than 5.0 (a common previous one is 1.4.2),
you will need to uninstall those Java programs before continuing. You can do this by selecting the
programs and clicking the "Change/Remove" button that appears and following the
directions. If the version number is 5.0, then you can skip the rest of this
installation chapter, as you have the newest version of Java running! If there is
no J2SE installation listed, or you have already uninstalled a previous installation,
simply keep reading!
The next step will be to download Java itself! You have a few options
on where to locate a working copy of Java. The first option is off the CD that comes
with your Lewis & Loftus Java book. Installing it is simple enough--follow the instructions for
the installer and you should be good to go. Note, however, that Java is not actually on the CD. Rather, it will come from the internet, so make sure that your computer has a network connection when you pop in the CD.
Your second option is to download Java off the Internet manually. To do so, direct your favorite
web browser to http://java.sun.com. Here, at Sun's
Java home page, you will find anything and everything on the Java programming language.
Most of what you'll find here, such as developer news and JVM specifications, you
probably won't have much interest in. However, what you are looking for is the Java
Runtime Environment, essentially the Virtual Machine, which will drive your Java
applications. Find the link below and click on it:
From the "Java 2 Platform, Standard Edition (J2SE)" drop-down list,
select the field below from the options available and click "Go":
From the menu that appears, you will have several options to choose
from, depending on what your familiarity with the Java programming environment is. For
the purposes of this tutorial (and CS 1322 in general), the first three options will be
the only ones of interest. However, should you wish to delve into the inner workings of
Java, the source code for the language is also available for download here, along with
some other utilities. Here are the files we are currently interested in:
J2SE 5.0 JRE
This is the Java runtime environment, which allows you to run Java
applications on your computer. It does not allow for the creation of Java
programs so it is not sufficient for our purposes, but using it allows a person to view Java applets in websites and run any other
programs which utilize the Java VM.
J2SE 5.0 JDK
This is the Java Software Development Kit. This is the download this
tutorial will follow. It includes the Java Runtime Environment as well as provides
the tools necessary for creating your own Java applications. However, to make
application creation even easier for you, there will be one more tool that we will
need to track down...
NetBeans IDE + JDK 5.0
An IDE is an Integrated Development Environment, which makes writing
Java applications a whole lot easier. If you like, you can download this package, which
also includes the Java SDK. However, this tutorial and class in general will be using
JGrasp as the preferred IDE, as it is both easy to use and relatively powerful. IDEs will be covered more thoroughly in a later section of this tutorial.
At this point, select the J2SE 5.0 JDK option for download.
Read through the license agreement, click "Accept", and then "Continue."
Now you will be at the download page. Here, you will have five choices
of computer types to choose from. As a Windows user, you will have a choice of
downloading and installing Java directly off the Internet, or downloading the installer
itself and installing Java without needing an Internet connection. The full online
installation uses much less hard drive space, but the choice is up to you. Both should
be nearly identical in process.
Save the file in a place that you will remember, sit back, and wait!
Once the download is complete, you should navigate through your file
system to whatever folder you actually downloaded the Java SDK into and double-click
on the installer. You'll pretty much just follow on the onscreen instructions, and you
can also most likely leave all the values at their defaults. However, at some point
it may ask you about multiple language support (which is up to you) as well as web
browser support. Whatever web browser you use, make sure you check its box, as this
will allow that browser to view applets.
Congratulations! You've installed Java. Now to configure it! .
Java Installation Overview - Macintosh
If you are running Tiger (Mac OS X 10.4.2), Java 5.0 is now
available for download. Go to (http://www.apple.com/support/downloads/java2se50release1.html)
and download and install the Java 5.0 package. After installing Java
5.0, you need to set this version as the default version. To this end,
double click on your Macintosh HD icon. From the window that pops up,
scroll down and find the folder titled "Applications" and then, within
this folder, folder "Utilities." Open up this folder and navigate
through it until you find the program called "Terminal." Fire up the
terminal, and at the prompt, type:
open -a /Applications/Utilities/Java/J2SE\ 5.0/Java\ Preferences.app
After typing this command, you should see the Java-preference
window. In this window, select J2SE 5.0 as the default for both
applets and the Java application runtime settings, and then save.
If you are running a previous version of Mac OS X (i.e., version
10.4.1 or earlier), Java 5.0 is not available for your
platform. In this case, we strongly suggest that you upgrade
to Tiger and install Java 1.5, to avoid problems. If you decide not to
install Tiger, we provide instructions on how to modify your Java
library to add to it a version of Java 1.5's Scanner class. Although
using such modified library should allow you to do homeworks that
would otherwise require Java 5.0, the provided Scanner class has
limited functionality, and we cannot exclude that it may behave
incorrectly in some cases. If you still decide to go for this
solution, follow the instructions at http://www.cc.gatech.edu/classes/AY2006/cs1322_fall/macscanner.html.
Now that Java is installed, we will configure it!
Configuring the Java Virtual Machine -
Windows Now we need to set up classpath and path
environment variables (don't worry about what these mean right now) so
that Java will be able to figure out where your code is in order to
make the applications. This actually isn't so much configuring
the VM itself as it is setting up Windows to recognize exactly where
the VM is. In order to this, you must once again access the Control
Panel. To save on having to scroll back earlier in this tutorial,
here is how to access the Control Panel.
|
1) Click on "My Computer." | |
2) On the left side near the middle of the panel, click "Control Panel."
|
Again, if the next window that comes up says "Pick a Category" across
the top, click the "Switch to Classic View" option to the right of it.
Now, click on the "System" icon. This should bring up a tabbed
"System Properties" window. Select the "Advanced" tab. Near the bottom of the window
will be a button with the title "Environment Variables." Click it, and from the list
of variables at the top, highlight the one that says "PATH."
Note: If the PATH variable is not present (though it really should be),
simply click the "New" button underneath.
If there is already something written in the "Variable Value" field,
put a semicolon ( ; ) at the end of whatever pathname is there, and then type the
following:
C:\Program Files\Java\jdk1.5.0\bin
The pathname that you type (default given above) should be the path
to wherever the "jdk1.5.0\bin" folder is. If you overrode the defaults during the JDK
installation, this folder may be somewhere else other than the example given here. If
this is the case, you should run a search for this folder and use the pathname the
search turns up. BE CAREFUL, as there are multiple "bin" folders, though technically
they should all work, this one is guaranteed to do so.
Now your system should be able to find the Java VM!
Configuring the Java Virtual Machine -
Macintosh
If you followed the instruction for Java 5.0 installation given
above, your Java should be already configured correctly. If you plan
to use Java from the command line, though, make sure to add to your
command path, as the first entry, the following path:
/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Commands
Consult the documentation for your command shell in case you do not
know how to modify your command path.
Overview and Installation of IDEs - Windows
As mentioned earlier, IDE is short for Integrated Development Environment.
Essentially, this means it's a nice and pretty and easy-to-use interface in order to
write all your code and subsequently run, test, and debug your programs. You could, technically, develop all
your code without an IDE. However, an IDE can greatly simplify many tasks, and for the purposes of this class, making it that hard on
yourself won't help you in the least. Here is a quick summary of the more well-known
IDEs out there.
An important note to remember: If you have never used an IDE
before (examples listed in bold below), we strongly recommend using the IDE
provided on the CD you can find in your book: JGrasp. Find its description in the
list of IDEs below for information on where to find JGrasp if you do not have a
copy of the CD in the book. Note that almost all the other IDEs have some quirks which make them tricky to use on our HW assignments, unless you really know what you are doing. Use them at your own risk!
JGrasp
JGrasp is a relatively simple IDE to use and has been designed to help out new java programmers. It comes with a built-in debugger, which you may find valuable a little later in the course.
This will be the IDE this tutorial focuses on. This is also the IDE that
CS 1322 officially supports. It can be found for download on the
JGrasp website, but you
can also install it directly off the CD that comes with your Java book. If you
are a CS major, you might want to look into an IDE with a little more power,
at least for future use. For the purposes of this class, this IDE and any others
listed here will do just fine.
JCreator
This is a favorite for most middle-of-the-road programmers. It is
quite easy to use, has a very friendly interface, and has a free version that can
be downloaded off the internet from the
JCreator webpage.
This IDE will satisfy the majority of programmers
out there, from the beginners all the way up to most recreational programmers ("Wait,
you mean people program for fun?..."). For most upper-end and corporate
developers, however, it becomes relatively underpowered.
NetBeans
This is the IDE which Sun offers in one of its downloads, packaged with
the Java Software Development Kit. It is a significant step above JGrasp in terms of
both learning curve and programming power, and also sports a graphical GUI builder,
which can make GUI assignments much easier to accomplish. You can download
NetBeans either from the Sun website, or from the
NetBeans website.
JBuilder
This IDE is along the same lines as NetBeans and is also available on
numerous platforms.
It boasts syntax-correcting help functions, easy plug-in capabilities with 3rd party
Java packages, and some powerful project management capabilities. For the average
programmer, this may be more than is needed, plus its learning curve is a tad steep.
However, this will satisfy most high-end and corporate programmers. JBuilder can
be downloaded off the
JBuilder
website. You may need to register to download JBuilder.
Eclipse
Probably the favorite among hardcore CS majors at Georgia Tech (ask
any CS 2335 student), Eclipse is the ultimate in both power and configurability,
sporting syntax and coding style correction plug-ins, graphical GUI editors,
automatic code and javadoc completion functions, and dozens more with its extensive
plug-in library. Its only downside is its learning curve, and its programming power
far exceeds what you will need for this course. However, if you plan on taking more
CS courses down the road, it is something you may at least want to have a look at eventually. It
is available for free download off the Eclipse
website.
As an interesting side note, it is worth mentioning that it is
entirely possible to write your Java files in plain text editors like Windows
Notepad. Additionally, if you are of the more ambitious type, famed text editors like
Emacs or vi (in which this help file was written!) can be used as well to generate
your source code. In fact, many students prefer actually typing in code initially through some text editor such as Notepad, emacs, or vi and then opening that file in JGrasp. (But make sure not to use Word!) Once your source text file has been opened in an IDE, it can be edited in
whatever ways you wish, compiled, and run.
Now that you are at least somewhat familiar with the more popular
IDEs out there, we'll now walk through the installation and configuration of JGrasp.
While this tutorial specifically uses JGrasp as its example (and CS 1322 officially
supports JGrasp), keep in mind that most IDEs, especially on the configuration level,
are very similar. Setting up the classpath and compiler options for JCreator is very
much akin to doing the same in JGrasp.
Installing JGrasp is pretty simple, just double-click on the
installer and follow the instructions. Once it's installed, now we can have some
fun! Fire up JGrasp. You should get a screen that looks something like this:
The largest white rectangle in the middle of the screen is where your
source code files will appear. The white box along the left side shows the current
folder that you have selected to store all your Java files (hint: classpath...). The
long bottom white rectangle is your compiler output and command line, which will be
covered later.
Before jumping into writing an actual class, however, you'll need to
configure JGrasp itself so that it knows where to find both the Java source files
and the Java VM itself. In the main window, under the "Settings" menu, choose "Path."
You should now be at a screen that looks something like this:
Those mysterious "Path" and "Classpath" words. Hopefully what you'll
do here will prevent you from ever having to deal with them again. As is shown in the
picture above, in the top text box you will type in the pathname to the Java VM (just
like you did in Windows' Control Panel), and in the lower box, you will type the path
to wherever directory it is that you will be storing all your .java files. The top text
box has the actual default path in it; the bottom you come up with on your own.
Once you have the correct values punched in, simply hit "Apply" and
you should be ready to build your first Java class!
If you are having classpath issues, the symptoms will look something
like this when you try to compile your source files:
Exception in thread "main": java.lang.NoClassDefFoundError
If this is the case, you may wish to set the classpath from the command
line, which is more difficult but more of a guarantee for success. From the
command prompt in Windows (MS-DOS), type:
set CLASSPATH=c:\myclasses\myclasses.jar
Replace the given pathname with whatever file path you choose to
save your files in. If you require different classpaths for different projects,
you can opt to set the classpath individually as per each project. When compiling
each project, you can add the following flags to the end to set the classpath:
javac -classpath "c:\myclasses\myclasses.jar;." my-java-program.java
Following this setup, you should be good to go!
Overview and Installation of IDEs - Macintosh
As mentioned earlier, IDE is short for Integrated Development Environment. Essentially, this means it's a nice and pretty and easy-to-use interface in order to
write all your code and subsequently run, test, and debug your programs. You could, technically, develop all
your code without an IDE. However, an IDE can greatly simplify many tasks, and for the purposes of this class, making it that hard on
yourself won't help you in the least. Here is a quick summary of the more well-known
IDEs out there.
An important note to remember: If you have never used an IDE
before (examples listed in bold below), we strongly recommend using the IDE
provided on the CD you can find in your book: JGrasp. Find its description in the
list of IDEs below for information on where to find JGrasp.
JGrasp
JGrasp is a relatively simple IDE to use and has been designed to help out new java programmers. It comes with a built-in debugger, which you may find valuable a little later in the course.
This will be the IDE this tutorial focuses on. This is also the IDE that
CS 1322 officially supports. It can be found for download on the
JGrasp website, but you
can also install it directly off the CD that comes with your Java book. If you
are a CS major, you might want to look into an IDE with a little more power,
at least for future use. For the purposes of this class, this IDE and any others
listed here will do just fine.
Eclipse
Probably the favorite among hardcore CS majors at Georgia Tech (ask
any CS 2335 student), Eclipse is the ultimate in both power and configurability,
sporting syntax and coding style correction plug-ins, graphical GUI editors,
automatic code and javadoc completion functions, and dozens more with its extensive
plug-in library. Its only downside is its learning curve, and its programming power
far exceeds what you will need for this course. However, if you plan on taking more
CS courses down the road, it is something you may at least want to have a look at eventually. It
is available for free download off the Eclipse
website.
Now that you are at least somewhat familiar with the more popular
IDEs out there, we'll now walk through the installation and configuration of JGrasp.
While this tutorial specifically uses JGrasp as its example (and CS 1322 officially
supports JGrasp), keep in mind that most IDEs, especially on the configuration level,
are very similar.
Installing JGrasp on a Mac is a little more involved than on a
Windows platform, but still quite simple. However, to my knowledge, the Mac version
is not available on the CD and will have to be downloaded off of the JGrasp website.
You will want to download the file that has been "g-zipped" and "tarred" (don't
worry about what these mean). Once you have downloaded this file, double-click on
your Macintosh HD icon and locate the file. By double-clicking on it you should be
able to extract it both from its g-zipped and tarred state, leaving you with
a folder like any other. Now launch its installer, follow the directions, and you
should be good to go!
Once it is installed, fire up JGrasp. You should get a screen that
looks something like this:
Granted this is a screenshot of JGrasp in Windows, but it looks
basically the same, since JGrasp was written in Java and is hence platform-independent!
Lucky for you Mac users, your classpath and path environment
variables are already set up! You can now move straight into writing your
first Java class.
If you are having classpath issues, the symptoms will look something
like this when you try to compile your source files:
Exception in thread "main": java.lang.NoClassDefFoundError
If this is the case, you may wish to set the classpath from the command
line, which is more difficult but more of a guarantee for success. From the
Terminal, open up your .bashrc config file and put the following into it:
CLASSPATH=/usr/j2ee/j2ee.jar:.;export CLASSPATH
Replace the given pathname with whatever file path you choose to
save your files in. If you require different classpaths for different projects,
you can opt to set the classpath individually as per each project. When compiling
each project, you can add the following flags to the end to set the classpath:
javac -classpath "./myclasses/myclasses.jar:." my-java-program.java
Following this setup, you should be good to go!
Writing a Java Class - Windows
Alright! You're in the home stretch for setting up Java and getting
your first application off the ground. Bear with me for just a little longer - you're
already well beyond the hardest parts (unless I screwed up somewhere and something
isn't working quite right, in which case you should contact your TA or instructor!).
From the "File" menu, create a New Java file. This should instantly
create a new text file in the large blank space of your JGrasp window. Type these
mysterious words into this text window:
public class Test {
public static void main(String [] args) {
System.out.println("Hello!");
}
}
|
My my, this sure looks mysterious! Let's see what this code means,
exactly. The first part:
public class Test {
This is a declaration. It is creating a class called "Test." A class,
in Java, is more accurately known as an Object. Think about Object intuitively, as you
would any actual tangible, physical object that does...something. A car, for instance,
could be an Object. All Object have attributes, or the specific aspects of the
Object that make it what it is. For a car, its attributes could be tires, seats,
power locks, XM radio, a turbocharged V6, a 6-disc changer...you get the idea.
Objects will also have member methods, or the functions that cause the Object's
attributes to interact together, or even with other Objects, to accomplish certain
tasks. For instance, with the assistance of roads and signals, and utilizing its
power steering, tires, and engine, a car can "drive."
For this example, we're keeping it simple. This class, or Object,
will not do anything other than print out some sentences.
An important thing to note, first. Java is picky about syntax.
Whatever you name the .java file (the file in which you will write your programs),
that must also be the name of the class itself (the word after "public class
____"), and it is case-sensitive.
Now back to our original text window. You should have typed in
the original example, and now you should save it. Go to "File" and select "Save As."
Type in "Test.java" and save wherever you feel appropriate (as a good rule of thumb,
save all your .java files for a single project in the same folder). Now, your window
should look something like this:
Now that you have the class name and file name matching correct, let's
continue with our analysis of this short block of code. The next part:
public static void main(String [] args) {
Without going into too much detail about the meanings of each
individual word there, just know that every project you write will need to
have at least one of these in it. When you run the project, this is what
Java will look for at the very beginning, and execution of your entire project will
start here. Next line:
System.out.println("Hello!");
Here, an Object is being used! It is an Object called "System,"
and we are using one of its member methods to do something. In this case, the
method is called "println" and it prints out to your monitor whatever is written
inside the quotation marks that are inside the parenthesis.
Remember, Java is very picky about syntax, so every curly brace you
open, you must close. Every opening paren must be matched by a closing paren. Also,
you need semicolons at the end of every line of execution code (as shown in the
example).
Now the moment of truth! Let's see if this sucker runs and does
what it should. From the "Compiler" menu, choose "Compile." The rectangle at the
very bottom of the screen should now be displaying some bizarre commands as the Java
compiler and Virtual Machine go to work. Should everything behave properly, you
should eventually see a "process completed" output appear, signaling that compilation
went fine and your Object is now ready for execution. From the "Run" menu, select
"Run," and you should see everything inside the println( ) parenthesis
print out into the rectangle! Experiment and change what is inside those parenthesis,
recompile the file, and run it again.
Congratulations! You have succeeded in writing you own Java class!
Now, to throw one final wrench in everything, let's try compiling and running this
class from the MS-DOS prompt! Why, you ask? This is always always always a
favorite test question among the CS 1322 instructors. "What is the command to
compile MyClass.java from the DOS prompt?" "What is the command to compile all
.java files from the DOS prompt?" Something like this always appears on
some exam. So, without further adieu, go to your "Start" menu and click "Run."
Type "cmd" and hit Enter, and a new window should pop up that is black and has
some gray depressing text on it. You will need to navigate to the directory where
you saved your Test.java file.
Type the command as you see it there, except replace everything
after the backslash ( \ ) with the path to wherever you saved Test.java. Hit enter
once you have done this. Now, type: javac Test.java and hit Enter.
Nothing much really happens. But now, type this: java Test
and hit Enter.
Whoa! It runs your program! Typing javac Test.java is
identical to clicking "Compile File" in JGrasp, and typing java Test
is identical to selecting "Run File." To compile all .java files in
the same folder, simply type javac *.java and press Enter. Now you can run
your Java applications from the command prompt!
Writing a Java Class - Macintosh
Interestingly enough, there are no differences between
writing a class / Object in Windows as opposed to Macintosh, as both use the same
JGrasp IDE to do so. The only difference comes in at the command line compilation
and execution. For the purposes of easy reading, all pertinant information about
writing a class, compiling it, and running it in JGrasp will be copied here.
Alright! You're in the home stretch for setting up Java and getting
your first application off the ground. Bear with me for just a little longer - you're
already well beyond the hardest parts (unless I screwed up somewhere and something
isn't working quite right, in which case you should contact your TA or instructor!).
From the "File" menu, create a New Java file. This should instantly
create a new text file in the large blank space of your JGrasp window. Type these
mysterious words into this text window:
public class Test {
public static void main(String [] args) {
System.out.println("Hello!");
}
}
|
My my, this sure looks mysterious! Let's see what this code means,
exactly. The first part:
public class Test {
This is a declaration. It is creating a class called "Test." A class,
in Java, is more accurately known as an Object. Think about Object intuitively, as you
would any actual tangible, physical object that does...something. A car, for instance,
could be an Object. All Object have attributes, or the specific aspects of the
Object that make it what it is. For a car, its attributes could be tires, seats,
power locks, XM radio, a turbocharged V6, a 6-disc changer...you get the idea.
Objects will also have member methods, or the functions that cause the Object's
attributes to interact together, or even with other Objects, to accomplish certain
tasks. For instance, with the assistance of roads and signals, and utilizing its
power steering, tires, and engine, a car can "drive."
For this example, we're keeping it simple. This class, or Object,
will not do anything other than print out some sentences.
An important thing to note, first. Java is picky about syntax.
Whatever you name the .java file (the file in which you will write your programs),
that must also be the name of the class itself (the word after "public class
____"), and it is case-sensitive.
Now back to our original text window. You should have typed in
the original example, and now you should save it. Go to "File" and select "Save As."
Type in "Test.java" and save wherever you feel appropriate (as a good rule of thumb,
save all your .java files for a single project in the same folder). Now, your window
should look something like this:
Now that you have the class name and file name matching correct, let's
continue with our analysis of this short block of code. The next part:
public static void main(String [] args) {
Without going into too much detail about the meanings of each
individual word there, just know that every project you write will need to
have at least one of these in it. When you run the project, this is what
Java will look for at the very beginning, and execution of your entire project will
start here. Next line:
System.out.println("Hello!");
Here, an Object is being used! It is an Object called "System,"
and we are using one of its member methods to do something. In this case, the
method is called "println" and it prints out to your monitor whatever is written
inside the quotation marks that are inside the parenthesis.
Remember, Java is very picky about syntax, so every curly brace you
open, you must close. Every opening paren must be matched by a closing paren. Also,
you need semicolons at the end of every line of execution code (as shown in the
example).
Now the moment of truth! Let's see if this sucker runs and does
what it should. From the "Compiler" menu, choose "Compile." The rectangle at the
very bottom of the screen should now be displaying some bizarre commands as the Java
compiler and Virtual Machine go to work. Should everything behave properly, you
should eventually see a "process completed" output appear, signaling that compilation
went fine and your Object is now ready for execution. From the "Run" menu, select
"Run," and you should see everything inside the println( ) parenthesis
print out into the rectangle! Experiment and change what is inside those parenthesis,
recompile the file, and run it again.
Congratulations! You have succeeded in writing you own Java class!
Now, to throw one final wrench in everything, let's try compiling and running this
class from the Mac Terminal! Why, you ask? This is always always always a
favorite test question among the CS 1322 instructors. "What is the command to
compile MyClass.java from the Terminal?" "What is the command to compile all
.java files from the Terminal?" Something like this always appears on an
exam. So, without further adieu, go to your Macintosh HD icon and navigate to your
Applications folder. From there, find the folder entitled Utilities. Inside this
folder, find the program called Terminal and open it up. You should see a screen
appear that looks very much like the Windows MS-DOS prompt, shown below:
Again, even though the screenshot is from Windows, both interfaces
are similar, and, interestingly enough, the commands used are identical!
You will need to navigate to the folder where you saved your
Test.java file. Type the command as you see it in the picture, except replace
everything after the backslash ( \ ) with the path to wherever you saved Test.java.
Hit Enter once you have done this. Now, type javac Test.java and hit
Enter.
Nothing much really happens. But now, type this: java Test
and hit Enter.
Whoa! It runs your programs! Typing javac Test.java is
identical to clicking "Compile File" in JGrasp, and typing java Test
is identical to selecting "Run File." To compile all .java files in
the same folder, simply type javac *.java and hit Enter. Now you can run
your Java applications from the Terminal!
Conclusion
Congratulations! You have successfully learned about, downloaded,
installed, and configured the Java Virtual Machine and software development kit, as
well as tied a fully-functional IDE in with it to ultimately produce a working Java
application! Not to mention, you can now look like an erudite programming guru
by compiling and running your source files from the command prompt.
This tutorial was meant to give some insight into getting an
introductory Java course off the ground, especially to those have had little or no
previous programming experience. I have almost always found the most difficult part
of the course not to be the homeworks or exams or quizzes, but rather
setting up everything that I need to complete all those assignments, and unfortunately,
it is this "setting up" information that seems the hardest to come by. To those in
the same boat as I, this is for you. Remember that there are also countless other
useful IDEs out there aside from the ones mentioned here.
I hope you have found this introduction to be useful. As you
become more comfortable in the Java programming environment, you will find most IDEs
to be friendly in terms of their flexibility with your goals for your programs. As a disclaimer, if you are having ANY problems doing anything
mentioned in this tutorial, do not be hesitant to ask any of your TAs, professors, or
even fellow students for clarification, as getting all this basic functionality to
work is crucial to successfully completing CS 1322. Even a week of dealing
with dysfunctional classpaths and sick Virtual Machines can set your integration
into CS 1322 back several weeks.
Good luck and good programming!
|