|
|
 |
|
Solaris Linux Environment - Writing, Compiling and Running Java Programs
Solaris Operating Environment
The instructions on this page are for the Solaris Operating Environment. Instructions for Sun Work Stations running the CentOS Linux Environment may be found on CentOS Linux Environment
1. Making Directories for your Programs
You first need to create directories for your programs. You may make these either through UNIX commands using the terminal window or by using the file manager. You should familiarise yourself with both methods.
1.1 Using the terminal window
- Open the terminal window.
You may do this from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button, clicking on Hosts in the workspace menu that appears and then clicking on This Host.
- Wait for the terminal window to open.
- Check that you are in your home directory using the UNIX command pwd, if not use the UNIX command cd to get to your home directory.
- It is sensible to make a directory for all your Java programming work, e.g. type the UNIX command
mkdir Java_Prog
Generally, you may call the exercise directories by whatever names you wish within the UNIX rules. It is usual practice to name directories with a name that indicates their contents, e.g. Java_Prog in the above case. A course lecturer may ask you to use specified names for some of your sub-directories if he or she is going to collect your course assignments directly from your home directory. In such cases it is important that you name the relevant directories exactly as requested paying attention to the case (upper or lower) of the letters of the specified names.
- To enter the directory Java_Prog, type the UNIX command
cd Java_Prog
- The UNIX command, ls, will list all the directories and files in the directory in which you are currently working.
1.2 Using the file manager
- Open the file manager.
You may do this from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button and then clicking on Files in the work space menu that appears. Then click on File Manager.
- Wait for the file manager window to open.
- File manager should open displaying the files in your home directory. If it does not, double click on your home directory icon.
- It is sensible to make a directory for all your Java programming work. Click on File on the menu bar. Then click on New Folder... in the drop-down menu. Type in the name of the folder, i.e. your new directory, in the dialogue box that now appears. It is helpful if you use a meaningful name e.g. Java_Prog. Now click on the OK button. A course lecturer may ask you to use specified names for some of your sub-directories if he or she is going to collect your course assignments directly from your home directory. In such cases it is important that you name the relevant directories exactly as requested paying attention to the case (upper or lower) of the letters of the specified name.
- To display the contents of the directory, Java_Prog, double click on the Java_Prog icon now displayed in your home directory. It will at this point be empty.
2. Writing your Program
You must write your program using a text editor. You may use any text editor available. The two text editors, on the Departmental Sun Work Stations, that we think most appropriate are the standard text editor and the XEmacs text editor. The former is a very simple editor and is easy to use. The latter has several options that facilitate the writing and editing of Java and C source codes.
2.1 Standard Text Editor
This can be called from the tool bar at the bottom of the screen or by placing the cursor on the screen background, clicking on the right hand mouse button, and then clicking on Applications in the work space menu that appears. Then click on Text Editor.
2.1.1 Creating and saving a new file
Type your program on the page that appeared on calling the editor.
To save the file:
- Click on File in the menu bar at the top of the Text Editor Window.
- Click on the Save As ... command in the pull-down menu that appears.
- A Dialogue Box now appears.
- Double click on the directory, in which you wish to save the file, in the left hand folder box within the dialogue box. You may need to work through the directories on the path to the directory that you require.
- Type the name of the file you wish to save in the box below the line, Enter file name:, within the dialogue box.
If you are saving a Java source code file remember:
- that the name MUST be the same as the class name and that this name is case sensitive
- to append the java extension, e.g. Exercise1.java NOT Exercise1
Click on OK Button in dialogue box.
Once you have saved a file this way you may use the Save command rather than the Save As command. This will save the file in the text editor in the directory defined in the last Save As ... Command or, on opening a file (see below) in directory from which the file was opened, assuming you have not then used a Save As command.
Save your file frequently when creating it or editing it. That way you do not loose a significant amount of work if you accidentally do something foolish and unintentionally close the editor or crash the computer.
2.1.2 Opening an existing file
- Click on File in the menu bar at the top of the Text Editor Window.
- Click on the Open... command in the drop-down menu that appears.
- A Dialogue Box now appears.
- Double click on the directory you wish to open in the left hand folder box within the dialogue box. You WILL often need to change to the directory in which you save your files. Otherwise they will most likely end up in your home directory and not in the Exercise directory you made earlier.
- Double click on file you wish to open.
You may now edit this file.
2.1.3 Identifying line numbers
You may continually monitor the line on which the cursor is placed. This option is valuable in editing a C program when the compiler has given the line numbers on which it errors have been found.
To continually monitor line numbers
- Click on Options in the menu bar.
- Click on Status Line in the drop down menu.
- The row number for the row on which the cursor is placed and the total number of rows now show at the bottom of the text editor window.
3. Compiling your Java Program
3.1 Compiling the Java source code
The UNIX command is javac, e.g
javac Hello.java
compiles a Java source code called Hello.java and, if successful, produces a class file called Hello.class.
Check any error messages and go back to the text editor to make changes if required then save and compile file again.
NB Always save the most recent version before compiling!
Check your directory contents using either the UNIX command ls or the file manager.
You should now have Hello.class (in the ls listing) or the Hello.class icon (in the file manager).
4. Running your Program
4.1 Running a Java Application
If you have compiled a Java source code, e.g. called Hello.java, for a Java application you should have a bytecode file Hello.class in the same folder. You can now run this application by typing the UNIX command:
java Hello
4.2 Running a Java Applet
If you have compiled a Java source code, e.g. called HelloApplet.java, for a Java applet you should have a bytecode file HelloApplet.class in the same folder.
You need an html file to execute this file, e.g.
The width and height are the dimensions of the window, in pixels, in which the applet will be run on the web page.
You may run the applet using either appletviewer or a web browser
4.2.1 Using appletviewer
Use the UNIX command appletviewer e.g.
appletviewer HelloApplet.html
4.2.2 Using a web browser
You may:
- Use a UNIX command line from the terminal, e.g. type HelloApplet.html
- Click on the relevant html file in your file manager
- Open the html file from within the browser
5. A few useful UNIX commands
You will find the following commands either essential or useful in compiling and running Java programs on the Departmental UNIX system.
| pwd | | Prints the path name of the working directory, i.e. tells you the directory in which you are working. See Section 1.1 above. |
| ls | | Lists the files in the directory in which you are working. See Section 1.1 above. |
| cd xxx | | Changes the directory in which you are working to the one named xxx. See Section 1.1 above. |
| mkdir xxx | | Create a directory called xxx. See Section 1.1 above. |
| javac | | Java language compile command. See Section 3 above. |
| java | | Java language interpreter command. See Section 4 above. |
| man xxxx | | Displays on the screen the listing, from the on-line UNIX manual, for whatever UNIX command you have typed in place of the word xxx. |
In addition, the following commands may be useful when compiling and editing (they are specific to the C UNIX Shell, which is the shell, i.e. the part of the UNIX system that interprets user commands and passes them on to the kernel, in the departmental UNIX system).
| !! | | repeats the last command. |
| !n where n is a previous line number | | repeats the command on line n. |
| !xx... | | repeats the last command beginning with xx... . |
|
This page is maintained by Michael Thomas Flanagan - last update: December 2007
Department of Electronic and Electrical Engineering, University College London - Torrington Place - London - WC1E
7JE - +44 (0)20 7679 7306 - Copyright © 1999-2007 UCL
Disclaimer |
Advanced Search |
|