Java FAQ

This page is intended to address local configuration issues surrounding the use of Java. Other web pages would be more appropriate if you are interested in either downloading the JDK or learning more about the JDK classes.

  1. How do I access Java on CS computers?
  2. Can I use Java remotely from a non-Linux computer?
  3. How do I get Java for Windows 9X/Mac/Linux?
  4. Where can I find more documentation on Java?
  5. Error: Public class X must be defined in ...
  6. Exception in thread "main" java.lang.NoClassDefFoundError:
  7. Error: Unknown method name.
  8. Java uses Reference semantics.
  9. What is the difference between == and equals to compare objects.


How do I access Java on CS computers?

Java is installed on the CS department's Linux and Solaris computers. For the remainder of this FAQ, we assume that you are using a Linux computer.

First check to see if Sun's JDK is already on your path by executing:

which javac
If Linux does not respond with the full pathname of javac, then you will need to fix your path by:
  1. Determine whether your set path directive is in your .cshrc or .login in your home directory. If the set path directive is in your .login, move it to your .cshrc file.
  2. Edit your .cshrc file (from 1 above) adding:
    /usr/local/jdk/bin
    
    to your set path directive.
  3. Then finally execute the command:
    source ~/.cshrc
    as appropriate from within an Xterm window.
  4. Now try the which command again.

Step 3 is a one-time only event; the next time you login the commands javac and java should be in your path.

[TOP]


Can I use Java remotely from a non-Linux computer?

No problem. Just remote login to your favorite Linux machine using ssh. This will give you full remote X Window displays to all your favorite X applications, including Java applets.

[TOP]


How do I get Java for Windows 9X/Mac/Linux?

The Java Development Kit (JDK) can be freely download from Sun. It is available for Windows 9X/NT, Solaris, and Linux. If you want to download JDK for non-Sun-supported platform, follow the link entitled Other Java technology ports.

[TOP]


Where can I find more documentation on Java?

[TOP]


Error: Public class X must be defined in ...

Make sure the file name and the class name are spelled exactly the same, including the use of lower versus upper case letters.

[TOP]


Exception in thread "main" java.lang.NoClassDefFoundError:

I suspect you just executed the command:
javac X.class
for some class X. An ls reveals that indeed X.class is in your current directory. This is a common error. The java command expects a class name, not a filename. Leave out the .class, i.e., enter the command:
java  X
where X is the name of your class.

[TOP]


Error: Unknown method name.

Suppose you have the following statement:
i = max(a, b);
Unless the method is part of the current class, the compiler does not know which method is being invoked. In this case, the statement should read:
i = Math.max(a, b);
In the case of a method in the current class, this is implied.

[TOP]


Java uses Reference semantics.

Unlike C++ (which uses copy semantics), Java uses reference semantics. Consider the following:
a = new Ref(...);
Ref b = a;
a.set(5);
A change to a also changes b since a and b are references to the same object.

This is what allows a method to change an object parameter, since the object parameter (a reference or pointer) is actually passed by value. Thus, a method can change the contents of an object parameter, but not which object the parameter references.

When you need a copy of an object, you must use its clone method.

[TOP]


What is the difference between == and equals to compare objects.

Always use the method equals to compare two objects. The operator == tests for pointer equality (i.e., do the objects point to the same thing). The equals method tests to see whether the two objects are equal (and may vary in definition from one object to the next).

[TOP]


Robert Noonan, noonan@cs.wm.edu
Sep 8, 2003