Notes on Java

The information on this page is deprecated. It is preserved for historical reasons, but should no longer be considered accurate for any managed systems.

If you are having trouble with Java, read this page.

The default Red Hat installation uses GCJ. You probably want to use Sun’s Java. The most current update of all three supported versions of Sun Java are installed on the public work stations. They are in:

The latest is at:

/usr/java/java/

If it isn’t, please send an email to guru at cs dot brandeis dot edu telling us which machine you were working on.

To use version 1.x of Sun Java, you need to have /usr/java/java1.*x*/bin/ in your PATH environmental variable.

In order to add java to your path, first determine your shell:

echo $SHELL

If it returns /bin/bash (default on most accounts) you need the following command to add Java 1.6 to your PATH for the current terminal:

export PATH="/usr/java/java/bin/:$PATH"

To make this change permanent for future sessions, you need to add this command to your ~/.bashrc file:

echo 'export PATH="/usr/java/java/bin/:$PATH"' >> ~/.bashrc

If echo $SHELL returns /bin/tcsh the command is:

setenv PATH "/usr/java/java/bin/:${PATH}"

The file you need to edit is ~/.cshrc:

echo 'setenv PATH "/usr/java/java/bin/:${PATH}"' >> ~/.cshrc

(If it returns anything else, you ought to be able to deal with this on your own.)

At this point you can log out and log back in again and java should work as you expect. To make sure, you can run:

which java

and it should return /usr/java/java/bin/java If it doesn’t, you might run into problems.


Here’s a little script you can use to set up a java path automatically:

if [ -d /usr/java ] ; then
    dir=`ls /usr/java | head -n 1`
    export PATH=/usr/java/$dir/bin:$PATH
else
    echo ""
    echo "No java installation detected. Please notify
guru@cs.brandeis.edu that \`hostname\` does not have java correctly
installed."
fi

Put this in your .bashrc file and it will find java (assuming java is installed correctly) for you automatically when you log in. .bashrc is executed whenever bash (the default shell) is run.

The script itself is pretty simple. The top line is a conditional and should be familiar since you have to be a computer science student to have this account. The only tricky bit is [ -d /usr/java ]. This is a test to make sure that /usr/java is in fact a directory.

If it is a directory, we’re going to get a listing of java installations and then pick one. The one we use is getting assigned to the variable dir. We do that in the next line. You’ll notice that we’re setting dir equal to something or other in quotes. Those `…` things aren’t actually quotes but backticks. They say execute the code inside them, and then use that value. The bash code inside the backticks shouldn’t be totally unfamiliar. ls /usr/java gets a listing of whats in the /usr/java directory. Instead of printing it to the screen we’re going to send that listing to another program with a pipe |. In linux you can use pipes to pass the output of one program into another program. In this case we’re passing the output into head, which is used to show the top n lines of text. In this case we’re using an n of 1. Fortunately ls returns alphabetically ordered results, so jdk will appear above jre in cases where both are installed. We’d rather have jdk java anyway, so using the top result is correct. So what that line does is makes a variable and gives it a value equal to the name of the first folder in /usr/java.

Up next we have export. Once again we’re working with a variable, but this time we’re updating an important environment variable rather than something local to this script. PATH is a list of folders, separated by colons. When you run a command in linux, your shell goes through each folder in PATH looking to see if your command lives in that folder. If so, it executes. If not, you get a file not found error. Often problems with java in the berry patch are due to users forgetting to set their path to java. Some machines (like the AMD64s) have different versions of java, and each version lives in a different folder. This is why we have to find that folder and update your path each time you log in to a berry patch machine. Anyway, like I said we’re going to be exporting PATH for later use, but what exactly are we setting it to? /usr/java/$dir/bin is going to be the bin file (where executable program files live) in the java directory we already found. Note that in bash when variables are referenced they are prepended by a dollar sign, just like in perl and PHP. We don’t want to lose access to all the other linux programs you know and love, so after the java folder we append the original PATH variable. If you’re wondering why we didn’t do $PATH:/usr/java/$dir/bin, there’s a simple reason for that. Your path is searched sequentially. We want the newly detected java to be the first java executable that comes up. Otherwise /usr/bin/java (which isn’t really java) would get executed before we could get to the end of PATH where the location of the real java can be found.

Almost done. Up next is else. For those of you who haven’t been CS majors long, else is what happens when the first condition isn’t true; when /usr/java isn’t actually a directory. All we want to do at this point is issue a warning that java isn’t useable. To print to the screen, we’re going to use echo. The first line simply prints a blank line so that the user (that’s you!) has an easier time noticing the next. If you’ve been paying attention the next line is self explanatory. In fact, most of it is self explanatory even if you haven’t been paying attention. It just prints to screen a little message saying that you should inform us that java is broken. The part you might not have gotten if you weren’t paying attention involves backticks. hostname will run the hostname program and insert the value that that returns into the echo statement. Hostname is a trivial little program that tells you what a machine is named. It is important to tell us what machine is having problems. Otherwise we get error messages like “java is broken,” or “I can’t log in,” both of which are pretty much useless since we don’t know what machine to go fix.

Last modified: September 6, 2007