Java and Linux: A Growing Friendship
Once upon a time, Java was persona non grata on Linux distributions.
None of the distributions would touch it, because the license that Sun released Java under was seen as incompatible with the GPL license that Linux is released under. Some distributions included alternative versions of Java, such as the GNU gjc Java compiler, but they often didn't operate in quite the same way as the "official" Java released by Sun. For most Java developers, Java was a download and install on top of a normal Linux install, and you could never depend on your customers or users having Java installed on their machines. In short, the barrier to entry was prohibitively high to most people.
Well, we now live in a bright new day with an OSI-approved license for Java, and it is rapidly becoming a standard part of major Linux distributions. But if you're new to Java, it's good to know what all the pieces do; and if you're a Java pro who's new to Linux, it's nice to know where all the bodies are buried. Let's start with some information for newcomers to Java.
Java is an example of a byte-code language. That means that the compiler for Java doesn't turn the source code into "native" machine code for whatever processor you're running. Instead it turns it into byte-code, a machine-independent instruction set that needs an interpreter to run. This isn't unique to Java, Microsoft's newer languages compile down to something called CLR which is very similar in nature to Java byte-code. The idea as expressed originally by Sun is "Compile Once, Run Anywhere." In other words, you can (theoretically) take a Java .class file (which contains Java byte code), or a JAR file (which is an archive of Java bytecode files) and run it on any machine that has a Java byte code engine.
Because you need a Java compiler to create the bytecode, and a Java bytecode interpreter to run it, there are really two pieces of the Java picture. People needing to develop and compile Java programs need the Java SDK (Software Development Kit.) If you just want to run code that's already been compiled and packaged up, you only need the somewhat smaller Java Runtime Environment (JRE). The JRE is also what you need to run Java applets inside of a browser such as Firefox.
So, how do you get Java onto your machine? As it turns out, the answer depends a lot on the Linux distribution you're running. For example, let's consider the latest release of Ubuntu, 8.04. Out of the box, if you type java -version, you'll get this helpful message:
james@vm2:~$ java -version The program 'java' can be found in the following packages: * java-gcj-compat-headless< * openjdk-6-jre-headless * cacao * j2re1.4 * kaffe * jamvm * gij-4.1 * gij-4.2 * sablevm Try: sudo apt-get install bash: java: command not found
There are certainly a lot of options there, unfortunately none of them will get you Sun Java 5 or 6, the most stable current releases that most software packages require. The closest you'll get is openjdk-6-jre-headless, which gets you the OpenJDK build of Java.
OpenJDK is the community edition of Java based on Sun's GPL release of the Java source code, but it's still not 100% there in terms of compatibility with the commercial Java products that applications like to run on.
In fact, it's trivial to install Sun's version of Java on Ubuntu, IF you know the right package name. For Java 6, it's sun-java6-jre for the runtime version, and sun-java6-jdk for the development kit. Now that we know this, we can just say:
james@vm2:~$ sudo apt-get install sun-java6-jdk Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: java-common odbcinst1debian1 sun-java6-bin sun-java6-jre unixodbc Suggested packages: equivs binfmt-support sun-java6-demo sun-java6-doc sun-java6-source sun-java6-fonts sun-java6-plugin ia32-sun-java6-plugin ttf-wqy-zenhei libct1 libmyodbc odbc-postgresql Recommended packages: gsfonts-x11 The following NEW packages will be installed: java-common odbcinst1debian1 sun-java6-bin sun-java6-jdk sun-java6-jre unixodbc 0 upgraded, 6 newly installed, 0 to remove and 102 not upgraded. Need to get 43.7MB of archives. After this operation, 130MB of additional disk space will be used. Do you want to continue [Y/n]? y
A few minutes of downloads, installs, and license acceptance windows later, and you'll have a working Java install, as we can demonstrate:
james@vm2:~$ java -version java version "1.6.0_06" Java(TM) SE Runtime Environment (build 1.6.0_06-b02) Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing
When you use the Ubuntu packaged version of the JDK, you'll end up with binaries in /usr/bin which link to /etc/alternatives, so that /usr/bin/java is a link to /etc/alternatives/java. This is to allow you to change the default version of Java that you're running easily.
But the fun isn't over yet. /etc/alternatives/java is a link to /usr/lib/jvm/java-6-sun/jre/bin/java, which is where Java was really installed.
You may find yourself running a distribution that doesn't package up Java for easy installation, or need to install a specific version. If so, you'll be forced to abandon your native package management system and install Java by hand. We'll assume that you're going to want to do some Java development as well as just running Java applets in your browser, so you're going to want to download the JDK (Java Development Kit), which you can find at http://java.sun.com/javase/downloads/index.jsp. You'll see a lot of options here, including a download with Netbeans and one with Java EE (Enterprise Edition), but you probably just want to grab the latest non-beta version of the JDK. When you click on the link to download, you'll be asked to pick a platform and language. Language is obviously a matter of preference, but the platform choice isn't quite as simple. The two choices you'll be offered for Linux are a 64 bit and 32 bit version of the JDK. You might assume that if you're running a 64 bit version of Linux for something like an AMD-based system, you should download a 64 bit JDK. You may find that it's more trouble that it's worth if you want to run Java plugins under Firefox, as an example. The whole question of whether you should ever install a 64 bit version of Linux most of the time, even on hardware that can support it, is still open to debate in many circles.
For the moment, we'll assume you've gone ahead and selected to download the 32 bit version of the JDK. Your next choice is one of packaging. The JDK is available in both RPM and binary installer formats. If you use an RPM-based distribution like Red Hat, Fedora or SUSE, using an RPM-based version of the JDK may allow you to uninstall or update the package more easily that using the binary package. For distributions that don't use RPM as their default package manager, use the binary JDK installer. Once you download the package, you'll either be installing it using RPM, or just running the download itself to install if you selected the binary installer. As an example, we'll return to the same Ubuntu system, and install Java using the Sun installer instead.
When you download the binary installer, it ends up as just a file on your desktop. You'll first need to make it executable, then run it as root. And because all the installer really does is unpack the files in place, you want to be in the right place before you run it. Most people like to use the /usr/java directory, a directory you'll have to create.
james@vm2:~$ sudo mkdir /usr/java james@vm2:~$ cd /usr/java james@vm2:~$ chmod a+x ~/Desktop/jdk-6u6-linux-i586.bin james@vm2:~$ sudo ~/Desktop/jdk-6u6-linux-i586.bin
The installed is going to print out a license agreement slightly longer than War and Peace on your screen, and then give you the option to accept it. Once Java is unpacked, you have two choices. You can either add /usr/java/jdk1.6.0_06/bin to your PATH variable, so that the compiler and byte code intepreter become available to you without having to specify the full path, or you can use the 'ln' command to make links from all the programs to the /usr/bin directory:
james@vm2:~$ sudo ln -s /usr/java/jdk1.6.0_06/bin/* /usr/bin
If you want to install Java using the RPM installer, you need to be using an RPM-based distribution, as mentioned. If you just take the defaults on Fedora 8, you'll get the JRE installed using the directory scheme (/etc/alternatives and /usr/lib) that Ubuntu uses. If you uninstall Sun Java, you'll be left with the GNU gjc version installed.
But if you make sure you don't have Sun's version installed and download the RPM installer. If you run the installer as root, it will install the JDK into /usr/java and set up links, just as we did with a manual install using the binary installer.
As you can see, the details of where and how Java gets installed can vary greatly from distribution to distribution. There are big advantages to using the "native" version of the Sun Java distribution if it's available, because then it will be updated automatically to the newest version whenever one becomes available. And as you can also see, most of the more recent distributions include Sun Java, or make it easy to install.
Once you've got Java installed, you can take it out for a test drive. Create a directory path "java/org/demo" in your home directory by doing:
[james@vm2 ~] mkdir -p java/org/demo
Now we can create a "Hello World" Java class and see if everything is set up right.
[james@vm2 ~] cat > java/org/demo/HelloWorld.java package org.demo;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hellow World!");
}
}
^D
[james@vm2 ~] cd java
[james@vm2 java] javac org/demo/HelloWorld.java
[james@vm2 java] java -cp . org.demo.HelloWorld Hello World!
If you're a seasoned Java developer, all of this should look familiar.
We've created a Java source file in the package org.demo, which lives in a directory structure that matches the package hierarchy. We compile the class with the javac command, and then run the "main" method by using the java command, specifying our current directory as the root of the class path.
Once you've got a copy of Java installed, there are a number of next steps you can take. To begin, you can install the Java plugin into Firefox, allowing Java applets to run on web pages. This is just a matter of linking a library file from the Java installation to the Firefox one, but it's different depending on your version of Firefox, so you should do a Google to find out the details.
You can also install the Eclipse development environment (as easy as "apt-get install eclipse" on Ubuntu, similarly easy in other distributions.) This gives you a state-of-the-art Java development environment. You can also install Tomcat, the Apache Software Foundation server that lets you develop Java-based web applications.
The Linux Standard Base effort has Java on their radar. As more distributions offer it as part of a base installation, it becomes more important to standardize how Java gets installed, but the LSB is going to leave this up to the individual distribution teams to work out.
However, it is becoming more and more likely that at least the JRE is going to become a required component of the LSB, so it's likely in the future that you'll find at least a Java runtime on your Linux install... somewhere...


