Section 6.2. Use Linux Features to Make Multiple Java SDKs Play Nicely Together


6.2. Use Linux Features to Make Multiple Java SDKs Play Nicely Together

We did not spend much time discussing the installation of Java on your Linux system in previous chapter. In fact, we did not discuss it at all. This is because the installation instructions that come with the SDK are more than adequate if you wish only to install a single Java SDK. But what if you want to install, say, both the Sun Java SDK and the IBM Java SDK? Then things get a bit more interesting.

We're going to review the concept of filesystem links, and how they work on Linux. While it may seem odd to discuss them here, we'll be using links to switch gracefully between different Java installations. Links are a powerful feature in Linux filesystems, and switching SDKs is a good application thereof. If you're already familiar with links, skip ahead to Section 6.2.2.

6.2.1. Links

A link is simply a name attached to a collection of datathat is, to a file. In other words, every file has one set of data and at least one link (a name). But a file may have more than one link. In other words, two different filenames may point to the same data. When you do this, you appear to have two copies of the file, but a change to one also changes the other. Deleting one, however, does not delete the other. It merely deletes the link. The file itself is only deleted when the last link is gone.

6.2.1.1 Hard Links

Links come in two flavors: hard and symbolic. A hard link looks like a file in and of itself. Let's show you a hard link, before and after.

 $ ls -la total 12 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul 8 10:11 . drwx------   50 mschwarz mschwarz    4096 Jul 8 10:11 .. -rw-rw-r--    1 mschwarz mschwarz      45 Jul 8 10:11 sample $ cat sample This is a sample file to demonstrate links. $ 

As you can see, we have a directory with a single file in it, sample. Now let's make a hard link to that file.

 $ ln sample example $ ls -la total 16 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:13 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw-r--    2 mschwarz mschwarz      45 Jul  8 10:11 example -rw-rw-r--    2 mschwarz mschwarz      45 Jul  8 10:11 sample $ cat example This is a sample file to demonstrate links. $ 

Notice a few things here. First, other than the size and timestamps being the same, there is nothing obvious to show that these two files are, in fact, the same file. Note also the number just ahead of the owning user and group names. In the first directory listing, sample had 1 in that position; now both sample and example have 2. This number is the link count. It tells you how many names are linked to the data associated with this name.

We have a couple more things to point out before we move on to soft links, which are going to be more important for our purposes.

 $ chgrp wwwdev example $ ls -la total 16 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:13 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw-r--    2 mschwarz wwwdev        45 Jul  8 10:11 example -rw-rw-r--    2 mschwarz wwwdev        45 Jul  8 10:11 sample $ chmod o-r example $ ls -la total 16 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:13 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw----    2 mschwarz wwwdev        45 Jul  8 10:11 example -rw-rw----    2 mschwarz wwwdev        45 Jul  8 10:11 sample $ chgrp mschwarz sample $ ls -la total 16 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:13 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw----    2 mschwarz mschwarz      45 Jul  8 10:11 example -rw-rw----    2 mschwarz mschwarz      45 Jul  8 10:11 sample $ 

As you can see, a file can have only one set of owners and permissions, no matter how many links are made to it. Changing the owner or permissions of one link changes all hard links at the same time. In other words, the security of a file is like its data: A change to one link is a change to them all.

A link need not be in the same directory as the original name.

 $ ln example /tmp/sample $ ls -la total 16 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:13 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw----    3 mschwarz mschwarz      45 Jul  8 10:11 example -rw-rw----    3 mschwarz mschwarz      45 Jul  8 10:11 sample $ ls -la /tmp total 132 drwxrwxr-x    2 mschwarz mschwarz    4096 Jul  8 10:23 . drwx------   50 mschwarz mschwarz    4096 Jul  8 10:11 .. -rw-rw-r--    1 mschwarz mschwarz  118081 Jun  3 18:51 jLin.tar.gz -rw-rw----    3 mschwarz mschwarz      45 Jul  8 10:11 sample $ 

Here we made a third link in a different directory; /tmp/sample is a third name for the same data file. Note that we made it from the example link, not the original filename. In fact, as far as the Linux filesystem is concerned, there is no "original" name. None of these names is more significant than any other. When you remove a filename, the link is destroyed and the file's link count is decremented. If the link count goes to zero, the file is removed. That's it. Nothing else.

Hard links have a couple of drawbacks. One of them is a genuine technical limitation and the other is more of a usability problem. The technical limitation is that a hard link cannot be made across mounted filesystems. In the simplest case (we don't want to muddy the waters with LVM[1] or RAID[2] at this pointmost Linux distributions do not do LVM or RAID "out-of-the-box"), if you have more than one partition or disk drive, these are "mounted" at different points on the directory tree. For example, Mr. Schwarz's laptop's mount table looks like this:

[1] Logical Volume Manager. This is a tool that lets you arbitrarily aggregate disk drives and partitions into a "logical volume" that may be mounted and unmounted as a unit. Such tools are commonly used in serious production servers, but are rare on workstations or simple Linux servers.

[2] Redundant Array of Inexpensive Disks. Another heavy server feature that allows multiple disk drives to be linked up as if they were a single disk drive and to act as backup to one another silently and transparently.

 $ mount /dev/hda2 on / type ext3 (rw) none on /proc type proc (rw) usbdevfs on /proc/bus/usb type usbdevfs (rw) /dev/hda1 on /boot type ext3 (rw) none on /dev/pts type devpts (rw,gid=5,mode=620) none on /dev/shm type tmpfs (rw) $ 

We have one large partition mounted at /, or root, and a small partition mounted at /boot. In all of our hard link examples so far, we have been making links on the root filesystem. Example 6.1 shows what happens when an attempt is made to hardlink between two different mounted devices.

This is what we mean when we say a link cannot cross filesystems.[3]

[3] In a similar vein, there are some networked filesystems that do not support hard links at all because the server or host system doesn't support the concept. Attempts to make links on or to such systems will also fail.

The other problem is more "touchy-feely." With a hard link, you can see by the link count that other links exist, but you don't know where they are. Symbolic links get you around both of these issues.

6.2.1.2 Symbolic Links, or Symlinks

In a sense, symbolic links are much simpler than hard links. A symbolic link is a file that contains the name of another file or directory. Because it is marked as a symbolic link, the system will replace it with the contents of the linked file. Example 6.2 will make this more clear.

Example 6.1. Attempt to hardlink between mounts
 $ ln example /boot/sample ln: creating hard link `/boot/sample' to `example': Invalid cross-device link $ 

Example 6.2. Symlinking /etc/passwd
 $ ls -la total 8 drwxrwxr-x    2 mschwarz mschwarz     4096 Jul  8 15:30 . drwx------   50 mschwarz mschwarz     4096 Jul  8 15:29 .. $ ln -sf /etc/passwd passwd $ ls -la total 8 drwxrwxr-x    2 mschwarz mschwarz     4096 Jul  8 15:31 . drwx------   50 mschwarz mschwarz     4096 Jul  8 15:29 .. lrwxrwxrwx    1 mschwarz mschwarz       11 Jul  8 15:31 passwd -> /etc/passwd $ cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ... etc. 

What we did here works just like a hard link, but note the attributes on the file: The l indicates a symbolic link, and the permissions are read/write/execute for user, group, and other (or for "world" for short). However, these permissions apply to the link, not to the file. Just as with hard links, there is only one set of permissions on the file, and these are on the file that is pointed to, not on the pointer.

One interesting difference between symlinks and hard links is that symlinks do not increment the link count on a file. If we remove /etc/password (a very bad idea, by the way), the symlink would be unaffected, but an attempt to open or read the symlink would not work, because it points at nothing that exists. This is called a dangling symlink.

Symlinks may refer to symlinks, and thus you need to be cautious to avoid circular symlink chains. All the shells we have used on Linux report circular symlinks as "too many levels of symbolic links," which sounds like a process exceeding its stack limits but handling it gracefully.

6.2.2. Switching Java Versions by Symlink

Here is the process we went through to install both Sun's Java 2 SDK and IBM's Java 2 SDK on a system at the same time.

1.

Download the Sun JDK as a compressed TAR file.

2.

Install it to /usr/java. The Sun installer named its directory j2sdk1.4.1_02, so the full path is /usr/java/j2sdk1.4.1_02.

3.

Download the IBM JDK and untar it also in /usr/java. The base directory in the TAR file was IBMJava2-141, so the path is /usr/java/IBMJava2-141.

4.

Create a symlink called jdk in /usr/java and make it point at the Sun JDK by default (Example 6.3).

5.

Add /usr/java/jdk/bin to the system's default PATH environment variable.[4] Also add JAVA_HOME and JDK_HOME environment variables that point to /usr/java/jdk.

[4] Exactly where you do this depends on your distribution. If you aren't sure, you can always do it in the .bash_profile file in your user account's home directory.

Now when we run javac or any other Java command, we run the version which is pointed to by the jdk symlink. If we wish to switch to the IBM JDK, we can just replace the link as show in Example 6.4.

From then on, the machine will be using the IBM Java SDK.

And, of course, by explicitly setting the path and environment variables, you can use whatever you prefer without changing the symlink for all other users on the system. This is an excellent example of how the features of the Linux system can make your life as a Java developer easierwith this general method, you can keep as many Java SDKs from as many vendors as you wish and switch between them at will.

Here, we showed you how to do this on a system-wide basis, but you could, by creating the symlink in your home directory and changing the path for your user account, switch between versions in your own account only, leaving the system-wide default alone. Linux provides endless flexibility for developers.

Example 6.3. Symlinking jdk to the Sun Java SDK
 # ln -s j2sdk1.4.1_02 jdk # ls -la total 16 drwxr-xr-x    4 root     root        4096 Jul  8 15:51 . drwxr-xr-x   17 root     root        4096 Jun 17 10:18 .. drwxr-xr-x    8 root     root        4096 May 21 21:09 IBMJava2-141 drwxr-xr-x    8 root     root        4096 Mar  5 14:44 j2sdk1.4.1_02 lrwxrwxrwx    1 root     root          14 Jul  7 22:33 jdk -> j2sdk1.4.1_02 # 

Example 6.4. Symlinking jdk to the IBM Java SDK
 # rm jdk # ln -s IBMJava2-141 jdk # ls -la total 16 drwxr-xr-x    4 root     root        4096 Jul  8 15:51 . drwxr-xr-x   17 root     root        4096 Jun 17 10:18 .. drwxr-xr-x    8 root     root        4096 May 21 21:09 IBMJava2-141 drwxr-xr-x    8 root     root        4096 Mar  5 14:44 j2sdk1.4.1_02 lrwxrwxrwx    1 root     root          14 Jul  7 22:33 jdk -> IBMJava2-141 # 



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net