Getting started with Java This is some simple advice on how to run your first Java program. We'll start with the simplest scenario, running Java on the gl.umbc.edu linux machines using standard Unix tools, and then give some brief advice on setting up your own machine to run Java. Running Java on gl.umbc.eduLog into a linux machine in the gl environment. The java compiler is javac and the java interpreter is java. We'll start by compiling and running a hello world application, so create a directory like ~/java/HelloWorld/ and cd to it. % cd % mkdir java % cd java % mkdir HelloWorld % cd HelloWorldNow copy the HelloWorld.java file into your directory. Compile the java source program, which will produce a java classfile HelloWorld.class % javac HelloWorld.java % ls -l total 2 -rw------- 1 finin 41 426 Oct 8 13:01 HelloWorld.class -rw------- 1 finin 41 115 Oct 8 13:00 HelloWorld.javaRun your java program using the java command: % java HelloWorld Hello World! % Fibonacci is another simple java program you can experiment with. COpy this into your directory. Compile it with javac and then run it with the java command. It prints out some initial numbers in the fibonacci series. A third simple java program you can examine, compile and run is Factorial.java. This program takes an integer argument from the command line and prints the factorial of that number. Note that the conventions for accessing command lines arguments is different than those used in C and C++. In Java, the command line arguments are stored in an array args. The number of arguments can be found using the length method of an array. See the code for an example. Here's a session: % javac Factorial.java % java Factorial 6 720.0 % java Factorial Usage: java FactorialFor a fourth example, consider a first Java assignment from a few years ago, computing Hailstone Sequences in Java, and a model solution. The solution directory has files making up a BlueJ project. The Hail.java file is the important one, of course. Installing Java on your own computer To use your own computer, you should install the following (free) software and documentation:
Note: The above software is available for Windows, Mac, and Linux, except that Java 1.5 is not yet available for MacOS X. For more information
|