|
Java Tutorial |
CommandLineJavaThese 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 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 Find out where your Java installation is (often something like Now to add this to the path temporarily use the following command (subsitute your path in this command): 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 Actually running a program Having got that sorted out we can now run something. Change directory to wherever your I can enter It may run or you may get a an error 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 For now we will tell Java the classpath directly 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 Note that the class was in a package called This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes |