Introduction to Java


How to install Java compiler in Linux

For Ubuntu Linux, you type the following in the terminal to install Java:
$ sudo apt-get install openjdk-6-jre
$ sudo apt-get install openjdk-6-jdk
$ sudo apt-get install openjdk-6-source
You can also install them from Ubuntu Software Center.

For Fedora Core, you will take the following procedure:
  1. Find the latest Java 2 SDK at the download page. Or you can find the rpm file (version 1.4.2_04) here. (You may have to right click it and select "Save link as" or "Save target as.")


  2. Go to the folder where you download the file. Become Super User and install it with rpm command as follows:
    $ su
    Password:
    # rpm -ihv j2sdk-1_4_2_04-linux-i586.rpm


  3. Go to the home directory with cd command. It is a folder labeled by your login name. Type "ls -a" in the terminal and find if there is a file named ".bash_profile" in the list. (This file is hidden and cannot be seen without the option -a.)


  4. You will edit this file so you can compile a java code anywhere. Use any preferred editor. The editor called "nano" is one of the easiest ones used in terminal. In terminal, type the command:
    # nano .bash_profile


  5. After opning the file, insert the following statement above the export command in the file.
    PATH=$PATH:/usr/java/j2sdk1.4.2_04/bin/
    Do not forget the slash after bin. The folder name, j2sdk1.4.2_04, depends on the version you download. Save the file and exit the editor.


  6. Then, exit from Super User. (Type "exit" in the terminal.)


  7. Update the file, .bash_profile, as follows:
    $ source ~/.bash_profile


  8. Type the following in the terminal.
    $ java -version
    If the version information is displayed, the instllation and setup are successful.


How to compile and run a Java program

There are particular rules for Java codes: Here is a simplest code to output a message:
class Test{
     public static void main(String[] args){
       System.out.println("This works!");
   }
}
The class name is "Test", so the file name has to be saved as Test.java. To compile it, use javac command in the folder where you saved the code.
$ javac Test.java
If there is no message after compiling it, the code does not have any error. This generates the class file, Test.class. To run it, type as follows:
$ java Test
The message, "This works!", will be displayed in the terminal.

How to make applets

An applet can be posted in a web page. There is a sample Java code to draw a message:
import java.awt.*;
import java.applet.*;

public class app extends Applet {
     public void paint(Graphics g) {
       g.drawString("The test has been succeeded!", 10, 30);
   }
}
The "import" command utilizes the package of classes. The package, java.awt, is for events of drawing, clicking, and etc. The package, java.applet, is for displaying the applet through a html file. The necessary statement is "extends Applet" after the declaration of the class. In the method of g.drawString("...", 10, 30), the last two values are the coordinate of the start position of drawing. You also need a html file:
<html>
<head>
<title>Test for applet</title>
</head>
<body>
<applet height=50 width=220 code="app.class"></applet>
</body>
</html>
The Java code has to be saved as app.java and the html file can be the same name as app.html.

Compile the Java code first:
$ javac app.java
Then, open the html file with an proper browser or run the compiled class file by using appletviewer command:
$ appletviewer app.html



Back to Electronics Page

Back to Hiro's Physics Main