<< Cool: Java WebStart Works On Debian GNU/Linux 4.0 AMD64 | Home | Let 6u10 Be Java 7 >>

Saturday Java Quiz: Know Your javac Command

Yesterday at lunch, I asked Charles: "It's Friday today, isn't it?" Charles said back: "That's sad."

Sadder still, even after the Friday-ness was confirmed, it didn't occur to me that I have been negligent of my Friday Java Quiz duties for two weeks in a roll.

So here goes, a day late, but just as punishing and irrelevant.

The set up: You have the Sun JDK (or OpenJDK) installed with javac and friends in your $PATH. You do not have $CLASSPATH set. And you have a bunch of jar files sitting in a directory, say, /usr/share/java. And you have written a class Foo.java in your home directory that uses a class in one of the jar files.

Q: Which of the following commands will successfully compile Foo.java:

  1. javac Foo.java
  2. export CLASSPATH=`echo /usr/share/java/* | tr ' ' :` ; javac Foo.java
  3. javac -classpath ".:/usr/share/java/*" Foo.java

You are allowed to check the man page for javac, but don't start to try out the commands before you have read the man page.

The command shown are Linux commands and as shown, won't work on Windows. Windows users should try the Windows equivalents of the above.

Tags :


Re: Saturday Java Quiz: Know Your javac Command

Neither I nor III will tell javac where the jar files are. III will only work if that directory contains the class file unpackaged. II creates a CLASSPATH with a list of the list files separated by the CLASSPATH delimiter for *nix.

Since you say Foo depends only on a class in one of the jar files, II should work not to have the current directory in CLASSPATH (which it isn't since you overrode the default).

The answer is II.

Re: Saturday Java Quiz: Know Your javac Command

That's what I thought until I read the javac documentation anew a few days ago.

This passage caught my attention:

As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.

According to this, III should also work.

Re: Saturday Java Quiz: Know Your javac Command

OK. That's where you caught me. That feature is new to Java 6, which I'm not using.

Add a comment Send a TrackBack