为什么CLASSPATH以JDK路径为前缀?

时间:2011-03-24 13:07:39

标签: java javah

在尝试构建使用javah的库时,我遇到了类路径问题。显然,类路径以JDK路径为前缀:请注意最终搜索路径错误。我该怎么办呢?

elmarco@makai:~/src/sasl/java/CyrusSasl (mingw32 *%)$ CLASSPATH=$PWD javah -o javasasl.h -jni -verbose Sasl 
error: cannot access Sasl
class file for Sasl not found
javadoc: error - Class Sasl not found.
[ Search Path: /usr/java/jdk1.6.0_24/jre/lib/resources.jar:/usr/java/jdk1.6.0_24/jre/lib/rt.jar:/usr/java/jdk1.6.0_24/jre/lib/sunrsasign.jar:/usr/java/jdk1.6.0_24/jre/lib/jsse.jar:/usr/java/jdk1.6.0_24/jre/lib/jce.jar:/usr/java/jdk1.6.0_24/jre/lib/charsets.jar:/usr/java/jdk1.6.0_24/jre/lib/modules/jdk.boot.jar:/usr/java/jdk1.6.0_24/jre/classes//home/elmarco/src/sasl/java/CyrusSasl ]
elmarco@makai:~/src/sasl/java/CyrusSasl (mingw32 *%)$ ls Sasl.java 
Sasl.java

感谢您的帮助!

(这是Fedora 14上的jdk1.6.0_24)

3 个答案:

答案 0 :(得分:1)

我通常会避开CLASSPATH环境变量。这应该有效(也许没有问题):

javah -classpath .;<your-path> -o javasasl.h -jni -verbose Sasl

如果除了本地路径之外什么都不需要,那么您不必指定-classpath选项,.是默认值。

注意 - 您必须先编译 Sasl.javajavah需要一个类文件。 (Getting started)

答案 1 :(得分:1)

我认为这只是javah如何输出其实际类路径的错误。会发生的事情是,它有很多地方可以搜索内置类,除此之外,它还使用$CLASSPATH中的内容。当它打印使用的实际类路径时,它们执行类似这样的操作(伪代码,假设implicitEntries是内置类路径条目的列表,explicitEntries$CLASSPATH中指定的目录的列表}):

print implicitEntries.join(pathSeparator) + explicitEntries.join(pathSeparator)

应该在哪里

print implicitEntries.join(pathSeparator) + pathSeparator + explicitEntries.join(pathSeparator)

以下对我来说很好:

$ ls
Sasl.class  Sasl.java
$ javah -classpath . -o javasasl.h -jni -verbose Sasl     [ Search Path: /usr/java/jdk1.6.0/jre/lib/resources.jar:/usr/java/jdk1.6.0/jre/lib/rt.jar:/usr/java/jdk1.6.0/jre/lib/sunrsasign.jar:/usr/java/jdk1.6.0/jre/lib/jsse.jar:/usr/java/jdk1.6.0/jre/lib/jce.jar:/usr/java/jdk1.6.0/jre/lib/charsets.jar:/usr/java/jdk1.6.0/jre/classes/. ]
[Creating file javasasl.h]
[search path for source files: [.]]
[search path for class files: [/usr/java/jdk1.6.0/jre/lib/resources.jar, /usr/java/jdk1.6.0/jre/lib/rt.jar, /usr/java/jdk1.6.0/jre/lib/sunrsasign.jar, /usr/java/jdk1.6.0/jre/lib/jsse.jar, /usr/java/jdk1.6.0/jre/lib/jce.jar, /usr/java/jdk1.6.0/jre/lib/charsets.jar, /usr/java/jdk1.6.0/jre/classes, /usr/java/jdk1.6.0/jre/lib/ext/dnsns.jar, /usr/java/jdk1.6.0/jre/lib/ext/localedata.jar, /usr/java/jdk1.6.0/jre/lib/ext/sunpkcs11.jar, /usr/java/jdk1.6.0/jre/lib/ext/sunjce_provider.jar, .]]
[loading ./Sasl.class]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Throwable.class)]
[loading /usr/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Class.class)]
[done in 585 ms]
$ ls
javasasl.h  Sasl.class  Sasl.java

现在,由于头文件生成似乎对你不起作用...你确定当前目录中有Sasl.class吗? javah适用于字节代码文件,而不是Java源文件。

答案 2 :(得分:1)

您在此处显示源文件可用...

(mingw32 *%)$ ls Sasl.java 
Sasl.java

但缺少的是类文件(Sasl.class)。

error: cannot access Sasl
class file for Sasl not found

首先编译它。