Downloading and Installing | Checking | First Program | Hints | Why .class? | Multiple JVMs | Back to Freestuff Home

Java

Java is free.  Free.  Java is a fully developed object oriented programming language that rivals C++ and has tons of tools and libraries for your programming projects.  Java has a few different IDEs that are also available free of charge.  Java was written with UNIX/Linux in mind, but also works on Windows machines and Apples.  Let's put it this way.  You can pay big dollars for a full featured version of Visual Studio and and then use Visual Basic, C Sharp (a blatent Java copy/glom/spinoff), ASP and C++ to create Windows Only apps.  Or... you can download Java for free, get Eclipse for free, get Tomcat for free and write code that's just a robust (if not more so), does the same thing and it runs on ANY PLATFORM YOU WANT

Not to bad mouth Microsoft, some things they do are great, but why would I pay cash money to use a lesser clone of a language and my apps only run on Windows?  Why, when the original is free, more powerful, has as many, if not more, libraries and tools as C++ and runs on Linux, Solaris, BSD, MacOS and Windows.  Why?  That, and there is a lot of Java code out there, old and new, so a decent idea of how Java works and what it can do will help you down the road, whether you intend to be an IT manager, an ivory tower professor type or an app developer.

Downloading and Installing Java

  We are going to go to SUN's Java site and download the JDK, or Java Developer's Kit.  This is going to allow us to write our own programs. We could get a JRE, that's the Java Runtime Environment, but that only lets you run java programs, it doesn't let you create and compile your own.  We want the JDK so we can develop our own applications.

First we'll create a directory for the setup file.  Go to Start -> All Programs -> Accessories and click on "Windows Explorer".  Once you've got a window like the one in the picture below (hopefully with less junk than I have) click on File, then New and create a new folder to keep temporary files in.  I always keep a directory called "Temp" for such things (as you can see in my listing).  Once you've created the directory we're ready to go get the install file from SUN.

 

 

Now, start up your web browser (I use Firefox as you can see, but Explorer works exactly the same for our purposes) and go to java.sun.com.  The page won't look exactly as I pictured it above, but the download section will be right where I'm pointing in red.  The first selection is "Java SE", the latest version, and is always the most popular.  Java has said goodbye to the J2SE names and version six is now Java 6 SE. (though the install will call the dir 1.6.0_02..)

 

 

It will bring you to the page shown here.  You're given a few options.  We're going to ignore the frills (you are offered to download the JDK with netBeans, an integrated development environment or IDE, all bundled together.  You also get offered to download the J2EE version which is the Enterprise Edition and only necessary later when you do more complicated stuff.) and just click on the link for - Download JDK 6 Update 2.  Go ahead and click on it...

 

** Next you get brought to a page ALMOST like the one above.  See the word "Required" is in red?  Click on the button next to the "Accept" and you will be brought to the page you see above. 

This is the page that has the link to the install file that we want to use to download the Java JDK install file.  Now, and this is a matter of personal taste, I never load software over the web if I can avoid it.  I always get the whole file and do the install "offline" (takes one variable out of the equation if something goes wrong during the install), so we're going to right click on the first listed link (the one I point to) and click on "Save Link As..." (in Explorer it's "Save Target As...) to download the entire file to our hard drive.

In Firefox and Explorer you should be asked where you want to save the file.  Navigate to the directory you created in the first step (In my case "C:\Temp") and save the file there.  It's a little under 60 megs so if you have a cable modem it's not too bad.  If you have DSL a little longer.  If you have dial-up you have my condolences.

 

(Some of the pictures below will be of a version 5 install... you get the idea though...)

After the download is complete, a peek into your directory with Windows Explorer should reveal the page above.  Make sure all your other applications are closed (Again, just to take variables out of the equation, you should always do this) and double click the file to run the install.

You'll get a yellow splash screen from SUN, a setup screen for InstallShield, all that good stuff, and then the install will begin.

 

When you get to the screen displayed above, hit "Next >"

You may get a screen asking you if you want the JRE with European language support.  Just click Yes...you're almost done...

After that you should get the screen you see above....

And Viola!  You now have the Java 2 Standard Edition JDK installed on your Windows machine.  We better make sure that everything went OK though, huh?  Click "Finish".

 

Checking that the Java JDK is installed and working

 

We're going to check things out with the Windows command prompt and some old school DOS commands, so lets get a command prompt window.  Click on Start and then Run.  When you get the little window that says "Open:" just type "cmd" and hit enter.  Viola.
(That's the cover of KISS' first album by the way.  s'got Strutter, Black Diamond, Firehouse, Cold Gin... go to iTunes and check it out)

 

Now to check to see if Java is installed.  At the command line type "java -version", hit enter, and you should see a display like what's above.

If you don't, if you see " java is not recognized as an internal or external command...blah, blah.." Then something has gone wrong with the install.  I would attempt to run the install again.  After that I would VERY CAREFULLY (and don't email me if you mess it up) delete the contents of "C:\Program Files\Java\<your java version ex. jdk1.6.0_02>" and then try to run the install again.  If that doesn't work I'd get a fresh copy of the install file, maybe it's corrupt, delete the directory and try it one more time.  After that I would give up and get some coffee, clear my head, and puzzle it out later. 

 

Writing that First Program

As you get more into Java you're going to see that we put programs in directories with the same name as the program.  It makes sense after all and you'll see that there's a few files that go into making a java program run.  So the first thing we're going to do, still from the command line, is create a directory that we can put our first program in.  Just type "cd \" and hit enter.  I don't show that, but it will take you to what's called "root" or the very top of your directory tree.

Then, and again this is what I do...your mileage may vary, create a directory called "MyJava" where you can keep all of your Java code.  You do that with the mkdir command (make directory).  Then you "cd" to the directory (cd is change directory, technically it's "chdir", but no one uses that anymore) and create a new directory for your program in the "MyJava" directory. (Yes, you are going to see these directories you're create when you go back to using Windows Explorer or My Computer later..)

  We'll use "mkdir" again and call that one "Hello".  Then we "cd" to "Hello".   Now, we're at C:\MyJava\Hello and we need to type in a program.  Any text editor will do, so we'll run "Notepad".  Just type "Notepad.exe" and hit enter.

 

A blank Notepad window will pop up.  Type in the program above.  DO NOT FORGET THE SEMICOLON.  Type it in just as you see it.  Then, when you're all done, click File, then Save As, and save the file as "Hello.java".  After you save it you will see the name of the file in the title bar like above.  Then close the Notepad window.  Your command prompt window will still be open.

 

 

Now, type in "dir" and hit enter.  "dir" is for "directory" and it just gives you a list of the files in that particular directory.  As you can see, you have one file, the one you just typed in, "Hello.java" (You can ignore those dots with the <DIR> in front of them for the time being).  Now we are going to use the java compiler to compile the program.  Just type "javac Hello.java" and hit enter.  If there are no errors, this will create a file called "Hello.class".  We do another "dir" to confirm this and we see it right above the listing for "Hello.java"

The last thing we do is run the program.  We do this by typing "java Hello" and hitting enter.  This finds the class file and runs the program, in this case outputing the string "Hello World" to the screen.

 Congratulations, you just installed a development package and created and ran your first java program!

If you don't want to use the command line and compile your programs like this there are a few IDEs, or Integrated Development Environments, that are more like Visual Basic and the development tools you're probably used to.  From the main freestuff page I have a link for Eclipse and Netbeans.  Both work great, both are free, and you can forget about the command line again.

Hints and Tips

Don't bother downloading the documentation.  It's always on the web and it's so dynamic that your version will be outdated in a week. 

JavaRanch is a decent place to get started and ask beginner questions.

SUN has an online tutorial to show you the basics.

Are you an Uber-Nerd?  Here then...

Books?

Only for the brave...

If you think this seems a little funny, this whole "class" file thing, let me explain...

This is how an old fashioned programming language used to work.  You wrote a program.  You translated your program into machine language (those 1's and 0's) that the computer can understand with a program called a compiler.  The compiler gives you "object" code, the ".exe" file, that can be run from the command line or have an icon point to it.  That's very simplistic, but that was the gist.  Sometimes you had a bunch of objects and you stuck them together with a thing called a linker.  Regardless, at the end of the day you had a file that, when ran, would perform the tasks you programmed it to do.

Other languages, BASIC for instance, didn't get compiled.  These programs were "interpreted".  Think of interpretation as being translated from regular text to machine code every time the program is run.  You ran your code through the interpreter every time you ran the program.  This was OK for simple things, it was convenient to just open a program file and read it, but you lost performance-wise.  Running object code is much quicker and was preferred.  That's how programming worked, with interpreters and compilers.

Problem was, if you compiled on a UNIX system you couldn't run the object code on Windows.  If you compiled on Windows you couldn't run it on an Apple.  Everyone had a different compiler and the same code didn't always work for every one of them.  Even today you know that a version of Word for Windows won't work on your Apple.  Programmers would have to write for a specific "platform" and then, if they wanted it to run on another system they would have to "port" their code (A nice way of saying jury-rig it however you could to make it work) for the new "platform" (Usually Windows to UNIX or vice versa).  This way of doing things sucked, never worked, and wasted countless man hours over the years.  That, and it assured that certain programs would never be used by certain users.  If you want to use iPhoto and all you have is a Windows machine then you, my friend, are screwed.  I have a PS2, so I have never played Halo.  This irks me, I've heard Halo is awesome, but because Microsoft will never port a popular XBox game over to a competitors system I'm screwed.  Same concept.  And no, I'm not buying an XBox just for Halo...

The "C" programming language was supposed to be the answer to this.  It had very few commands, a rich standard library and let you do high level and low level programming.  It was touted as a "portable" language.  Problem was that Borland and Microsoft wouldn't let ANSI standards be left alone, so even "C" became "un-portable" after a time as companies began to add proprietary pieces to what was supposed to be a standardized language.  By the eighties "C" was just as messed up as all of the other languages (See Microsoft's MFCs or Borland's OWL).

Java was built to be small.  It was originally made to run in cable switch boxes and other mini electronic devices, so being small and portable was a must.  What the developers did (two guys named Gosling and Naughton) was come up with a VM program, or a virtual machine to run their code.  This way they could write the same code for all the devices (Panasonic, Sony, Toshiba, etc) and each device would have it's own VM program to interpret the code the right way for that particular piece of gear.  Language stays the same, the way it's run on the machine is unique. 

They tried to sell the technology for a while and had no luck.  Then the Internet became popular in 1994 and they decided that Java would be a good language to create a browser with.  They created the "Hot Java" browser and became popular for running their mini programs or "applets" inside the browser.  It was so popular that Netscape Navigator, the most popular browser before Microsoft beat it to death, came "java-enabled".  Java took off and SUN built upon it and soon it became a development language to rival C++ in popularity and usage.

So, the point of all this?  When you ran "Hello.java" through javac, you were compiling to what's called java byte code (icode or intermediate code in the compiler maker's world).  That's the class file.  Then, when you run java on "Hello", you are telling the JVM, the Java Virtual Machine for Windows XP, to interpret your class file and run the program.  Theoretically, I could take the same Hello.class file and run it on an Apple or a Linux machine, because their JVMs would be made for those operating systems.  That's the way it works.  If you went back and looked at the download page you would see that there are JVMs for Solaris, Linux, and Windows.  Mac OS X comes with a JDK and JVM already loaded.

So, if someone asks you whether Java is compiled or interpreted you can honestly say "Both".  It's really considered interpreted to be technical, but there are products that will compile your java to object code (Cosmo code was one) if you want the speed at the cost of portability.  That's why you have the extra step and the class file.  It's why SUN's tag line for Java is "Write once, Run Anywhere".  Each different platform has a different JVM.  The same code should run on all the different platforms because it's the JVM interpreting the program that's specific to the platform, not the program code itself.  Now ya know.

Multiple JVMs?

If you're new to java don't read this.  Go away.  If you aren't then you're wondering why you reset your JAVA_HOME environment variable to use an old version and nothing changed.  What's going on?  The 5.0/1.5 setup copied the 5.0/1.5 java.exe, javac.exe, javaw.exe and javaws.exe to C:\windows\system32, so if that directory appears in your $PATH before your $JAVA_HOME/bin/ (it does) you are going to run 5.0 until the wheels fall off.  Put the dir for the JVM you want to use before the system32 dir in your PATH and your problems will be solved.  You can also use an absolute path, like C:\Program Files\Java\jre1.4.2_07\bin\java.exe to run the code with an old JVM as well.  If you mix and match you'll get fried, like if the java.exe from 1.5 sees 1.4 in the registry you'll crash and burn (you can have 1.5 in the registry and run all the others though, do that if you must), so try to keep only one JVM on your system at a time and make it the most current.

If you have any questions, suggestions, or corrections please let me know... 

biglars@cs.rutgers.edu

© 2007 Rutgers University