1
votes

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!

2
What kind of IDE are you using ? Doesn't it handle that for you ? And what happens, your build fails ? Have you got errors ?Colin Hebert
I'm just curious, why you don't run it using -cp argument ? "java -cp poi.jar:yourJar.jar YourClass"Dani Cricco
I'm not using an IDE at the moment. I really just wanted to whip something up... Well, at least see if POI can do what I need.Brad G
I had tried using the -cp command, but as Nathan is implying below, I have to include the full jar paths. I was only specifying the directory that contained the jars.Brad G

2 Answers

2
votes

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
1
votes

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 :)