views:

63

answers:

2

Hey guys,

I'm trying to do something very simple. All I want to do at this moment is build a file that contains the appropriate classes.

I have a file called Promomon.java

class Promomon {
  public static void main(String[] args)
  {
    System.out.println("Hello World!");
  }
}

Simple simple, everything is fine there. I can compile and run and I see Hello World!.

Now I add the classes that I wish to use.

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

That was taken straight from the Apache POI docs.

  • I am using Ubuntu 10.04. I installed java using synaptic (apt-get install openjdk-6-jdk)
  • Java is installed at /usr/lib/jvm/java-6-openjdk/jre/
  • JAVA_HOME is set to "/usr/lib/jvm/java-6-openjdk/jre/"

    $ echo $JAVA_HOME
    /usr/lib/jvm/java-6-openjdk/jre/

  • I built POI jars myself using Ant, no problem there.

  • I placed the jars into java's lib directory. (/usr/lib/jvm/java-6-openjdk/jre/lib/)

    $ ls -l /usr/lib/jvm/java-6-openjdk/jre/lib/poi*
    -rw-r--r-- 1 root root 1539296 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-3.6-20100908.jar
    -rw-r--r-- 1 root root 69142 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-contrib-3.6-20100908.jar
    -rw-r--r-- 1 root root 181907 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-examples-3.6-20100908.jar
    -rw-r--r-- 1 root root 412788 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-ooxml-3.6-20100908.jar
    -rw-r--r-- 1 root root 3774336 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-ooxml-schemas-3.6-20100908.jar
    -rw-r--r-- 1 root root 795893 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-scratchpad-3.6-20100908.jar

  • My classpath is set to this directory.

    $ echo $CLASSPATH
    /usr/lib/jvm/java-6-openjdk/jre/lib/

What on earth am I doing wrong? I also tried using the pre-built binaries, no change.

Thanks for you help!

A: 

Putting the jar files directly in the lib directory isn't going to help. You could put them in jre/lib/ext, or you could specify another ext directory (javac -extdirs=... and java -Djava.ext.dirs=...), or you could specify the jar files explicitly.

EDIT: I've a sneaking suspicion something changes to make this easier with Java 6, but I can't remember what. The above should sort you out until someone points out what I've forgotten :)

Jon Skeet
+2  A: 

When you specify jars in the classpath you can't just specify the directory they're in, you need to list the jars explicitly (the change Jon Skeet refers to in jdk6 may be a change this but I don't remember it either).

I wouldn't put those jars in the jdk lib directory. Make a lib directory next to the src directory for your Promomon.java file, add your jars to that, and list the jars individually in the classpath when you compile and run. And when I say classpath I don't mean the environment variable. Here are some alternatives:

  • create a couple of executable files that contain the compile and run commands and add the jar files to the list of stuff in the -cp switch.
  • Get ant and make a build.xml file.
  • Use an IDE like eclipse and specify the jars (in the Eclipse menu pick File->BuildPath->Configure Build Path and pick the "Libraries" tab).

Going from "Hello World" to a program that uses third-party libraries is a big step with Java. The simple approach to getting something compiled and running stops working fast.

BTW I looked it up here and the JDK6 change is that you can use wildcards in the classpath, so instead of listing the jars individually like

-cp=/usr/myapp/classes:/usr/myapp/lib/foo.jar:/usr/myapp/lib/bar.jar

you can say

-cp=/usr/myapp/classes:/usr/myapp/lib/*.jar
Nathan Hughes
Thank you! This fixed it. However I wasn't able to include the set of jars with the wildcard. I have to individually include each path. Nonetheless, it compiles now, thanks again.
Brad Goss
@Brad, i haven't tried using wildcards yet, also haven't used openjdk. either i misread something in the doc or maybe openjdk is not compliant on this just yet. glad it worked for you.
Nathan Hughes
It seems that I have to include the full paths to compile, but I can use wildcards when running.
Brad Goss