Recent Changes - Search:

Java Tutorial

edit SideBar

CommandLineJava

These pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial.

A Java Program can run on a wide range of different computers (PCs, Macs, Unix boxes, VAXs, Mainframes) and on a wide range of operating systems. A side effect of this is that a Java Class does not normally run "natively" - e.g. if you double click a class file in Windows explorer it will not execute the program.

Java relies on a Runtime Environemnt which acts as a kind of "adaptor" between the java program and the Computer/Operating system on which it is running. This is often referred to as a Virtual Machine (Virtual because it is like an imitation computer running inside a real computer).

So to execute a Java Program we need a Runtime Environment (or Java Development Kit, JDK, which includes the Runtime) - free from http://www.java.sun.com. Once downloaded and installed it is possible to run a Java class. There are usually a few things to sort out before it all works smoothy though. The Following instructions are for Windows. Instructions for other Computer/Operating Systems can be found through Google.

Open the command prompt (click on the Start button at the bottom left of the screen and choose Run. In the pop-up box enter cmd

this will get you a Command Window. First a quick test - type java and press enter. If you get a message that starts Usage: java [-options] class [args...] then the Runtime is installed and "on the path" so you are in business.Note about the Command Window.

If you got 'java' is not recognized as an internal or external command ... then it could be one of two things, either the Java Runtime Environment (JRE) is not installed (get it from free from http://www.java.sun.com) or it is not on your path.

The path environment variable is where the system will look to find programs to run (A path is the folder heirarchy from c: [or d: or whatever] to the folder that contains the program to run). What we need to know is if the JRE is "on the path" at the command prompt enter set path this should return something like
path=c:\somefolder\anotherfolder;c:\moreFolders\evenmore

Find out where your Java installation is (often something like
C:\Program Files\Java\jre1.5.0_07 and make a note of it (Windows search may help if you are having trouible). What we need is the path to the bin driectory in the jre (or jdk) installation. So in my case it would be
C:\Program Files\Java\jre1.5.0_07\bin
don't forget the bin on the end

Now to add this to the path temporarily use the following command (subsitute your path in this command):

   set path=\path\to\your\java\bin;%path%

What this does is define the path environment variable as having the path to your JRE and then, what ever was on the path tagged on the end. Note the semi-colon ; used as a separator.

Now you should be able to type java and press enter and see a "usage" message.

Actually running a program

Having got that sorted out we can now run something. Change directory to wherever your VatBill.class file is (The search facility of your operating system may help) using the cd command. In my case I would enter
cd \_work\java\tutorial

I can enter dir to list the files and check that the VatBill.class file is there. Now enter
java VatBill
note there is no .class on the end

It may run or you may get a an error
Exception in thread "main" java.lang.NoClassDefFoundError: VatBill

This error is most often due to a classpath error. Just like the operating system uses the path to find files Java uses a classpath to find classes. the classpath can be set using set classpath=some\path in a similar way to setting the path described above.

For now we will tell Java the classpath directly
java -cp . VatBill
the -cp . tells java the the classpath (-cp) to use is . Dot means the current folder, whatever it is.

Running a class that is in a package

One final thing to know, if, like our FileInfo class, the class is in a package then we need to position ourselves in the directory above where the package starts (again in my cas this is \_work\java\tutorial and then enter
java -cp . tutoral.files.FileDetails \path\to\a\file

Note that the class was in a package called tutoral.files.FileDetails so we had to enter the full package name (which corresponds to the directory structure tutoral\files\FileDetails) and I added an Argument of the name of a file for which i wantd details.

 

Creative Commons License

This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes
under the Creative Commons license Creative Commons Attribution-Noncommercial-Share Alike.

Edit - History - Print - Recent Changes - Search
Page last modified on May 18, 2007, at 02:15 PM