How to Compile and Run JAVA programs in UNIX: An Incremental F.A.Q.

Fall 2004
Somo



1. How can I access an UNIX environment?


2. Is jdk installed on the UNIX server?


3. How do I set up the JAVA environment?


4. How do I write a JAVA program in UNIX?


5. How do I compile a JAVA program in UNIX?


6. How do I execute a JAVA program in UNIX?


7. Do I need to set/modify the environment variablese,g., Class Path?






1. How can I access an UNIX environment?

Ans. In order to access an UNIX environment, you have to log on to one of the UNIX servers, namely aludra.usc.edu. You can use telnet or SecureShell (SSH) for this purpose. Once connected, you can enter using your usc login and password. You are now in the UNIX environment.


Return to Top




2. Is jdk installed on the UNIX server?

Ans. The Java Development Toolkit (jdk) is installed on this server. The default version accesible is currently Java 1.2.1, and the most recent version is jdk 1.4.2, which will be used in this class.


Return to Top




3. How do I set up the JAVA environment?

Ans. Before you can compile a JAVA program in the UNIX environment, you must set up the JAVA environment by triggering the jdk setup file. Here are the steps to do it:
1. Open your .login file for editing e.g., by typing
> pico .login
if you are using a pico editor.

2. Go to the very end of the .login file and add the following code in there (Make sure you do not make any other changes)
###########################################################################
#
# Source the jdk files
#
if (-r /usr/usc/jdk/1.4.2/setup.csh) then
        source /usr/usc/jdk/1.4.2/setup.csh
endif
###########################################################################
3. Save the modified .login file.

4. Now you need to source the modified .login file by typing
> source .login
These above-mentioned steps are required to be done only once.

Now you can start compiling your JAVA programs.


Return to Top




4. How do I write a JAVA program in UNIX?

Ans. In order to write a JAVA program in the UNIX environment, you need to open an editor. Few popular editors are vi, pico, and emacs. You can use any of these editors, whichever your favorite is, to write your java programs and save them.


Return to Top




5. How do I compile a JAVA program in UNIX?

Ans. In order to compile a JAVA program called Prog_Name.java, you have to type
> javac Prog_Name.java
at the UNIX prompt. Of course, you have to be in the directory where this program is located. This will create the .class file for your program, and you can see a new file in the directory called Prog_Name.class.


Return to Top




6. How do I execute a JAVA program in UNIX?

Ans. In order to execute an already compiled JAVA program called Prog_Name.java, you have to type
> java Prog_Name
at the UNIX prompt. Again, you must be in the directory where your program was compiled.


Return to Top




Do I need to set/modify the environment variablese,g., Class Path?

Ans. NO. The environment variables are set for you, and you don't have the need, nor the required permissions to change anything.


Return to Top




The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees