导入罐子不起作用
我正在尝试做一些非常简单的事情。此时我想做的就是构建一个包含适当类的文件。
我有一个名为 Promomon.java 的文件,
class Promomon {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
简单简单,一切都很好。我可以编译并运行,然后我看到 Hello World!。
现在我添加我想要使用的类。
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;
这是直接取自 Apache POI 文档的。
- 我使用的是 Ubuntu 10.04。我使用 synaptic 安装了 java (apt-get install openjdk-6-jdk)
- Java 安装在 /usr/lib/jvm/java-6-openjdk/jre/
JAVA_HOME 设置为“/usr/lib/jvm/ java-6-openjdk/jre/"
$回显$JAVA_HOME
/usr/lib/jvm/java-6-openjdk/jre/我自己使用 Ant 构建了 POI jar,没有问题。
我将 jar 放入 java 的 lib 目录中。 (/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根根3774336 2010-09-08 12:40 /usr/lib/jvm/java-6-openjdk/jre/lib/poi-ooxml-schemas-3.6-20100908.jar< br> -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我的类路径设置为该目录。
$ echo $CLASSPATH
/usr/lib/jvm/java-6-openjdk/jre/lib/
我到底做错了什么?我也尝试使用预构建的二进制文件,没有任何变化。
谢谢你的帮助!
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.jarMy 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在类路径中指定 jar 时,您不能只指定它们所在的目录,您需要显式列出 jar(Jon Skeet 在 jdk6 中提到的更改可能是对此的更改,但我也不记得了) 。
我不会将这些 jar 放在 jdk lib 目录中。在 Promomon.java 文件的 src 目录旁边创建一个 lib 目录,将 jar 添加到其中,并在编译和运行时在类路径中单独列出 jar。当我说类路径时,我并不是指环境变量。这里有一些替代方案:
从“Hello World”到使用第三方库的程序对于 Java 来说是一大进步。编译和运行某些东西的简单方法很快就会失效。
顺便说一句,我在此处查找了它 这样单独列出 jar
JDK6 的变化是您可以在类路径中使用通配符,因此不必像您
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:
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
you can say
将 jar 文件直接放在 lib 目录中是没有帮助的。您可以将它们放在 jre/lib/ext 中,或者您可以指定另一个 ext 目录(
javac -extdirs=...
和java -Djava.ext. dirs=...
),或者您可以显式指定 jar 文件。编辑:我偷偷怀疑 Java 6 做了一些改变,使这变得更容易,但我不记得是什么了。上面的内容应该可以帮助您解决问题,直到有人指出我忘记了什么:)
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=...
andjava -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 :)