如何调试为什么javac无法使用-classpath选项中的有效路径加载库?

huangapple go评论64阅读模式
英文:

How to debug why javac is unable to load a library with a valid path in -classpath option?

问题

我正在尝试使用Apache的PDFBox库。由于某种原因,javac无法加载pdfbox-2.0.28.jar。当我在Bash中运行以下任何一个命令时:

javac -cp "pdfbox-2.0.28.jar" MyPdf.java
javac -cp ".:pdfbox-2.0.28.jar" MyPdf.java
javac -cp "pdfbox-2.0.28.jar:." MyPdf.java
javac -sourcepath "pdfbox-2.0.28.jar" MyPdf.java

我会得到这个错误:

MyPdf.java:1: error: package org.apache.pdfbox does not exist
import org.apache.pdfbox.*;
^
1 error

我知道这个jar文件没有损坏,因为我可以轻松提取其中的内容。该jar文件中的类文件也存在。MyPdf.java是一个简单的Java代码,内容如下:

import org.apache.pdfbox.*;

public class MyPdf {
    public static void main (String... args) {}
}

我的目录结构如下:

pdf
├── MyPdf.java
├── pdfbox-2.0.28.jar
└── sample.pdf

pdfbox-2.0.28.jar文件的文件列表如下:

$ unzip -l pdfbox-2.0.28.jar
Archive:  pdfbox-2.0.28.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
    18161  2023-04-10 10:05   META-INF/MANIFEST.MF
        0  2023-04-10 12:05   META-INF/
     1655  2023-04-10 10:04   META-INF/DEPENDENCIES
    23477  2023-04-10 10:04   META-INF/LICENSE
      638  2023-04-10 10:04   META-INF/NOTICE
        0  2023-04-10 12:05   META-INF/maven/
        0  2023-04-10 12:05   META-INF/maven/org.apache.pdfbox/
        0  2023-04-10 12:05   META-INF/maven/org.apache.pdfbox/pdfbox/
      134  2023-04-10 10:05   META-INF/maven/org.apache.pdfbox/pdfbox/pom.properties
    43315  2023-04-10 10:04   META-INF/maven/org.apache.pdfbox/pdfbox/pom.xml
        0  2023-04-10 12:05   org/
        0  2023-04-10 12:05   org/apache/
        0  2023-04-10 12:05   org/apache/pdfbox/
        0  2023-04-10 12:05   org/apache/pdfbox/contentstream/
      431  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDContentStream.class
     7327  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDFGraphicsStreamEngine.class
    25077  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDFStreamEngine.class
        0  2023-04-10 12:05   org/apache/pdfbox/contentstream/operator/
     ... 省略部分 ...
     1514  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/ByteTrie$ByteTrieNode.class
     2585  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/ByteTrie.class
     1880  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/FileType.class
     3308  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/FileTypeDetector.class
---------                     -------
  5603142                     879 files

我查看了来自这个网页搜索查询的一些搜索结果,包括这里的一个,但都没有解决我的问题。

我如何找出我做错了什么?这不是我第一次使用classpath加载库,但我无法使这个东西工作。

英文:

I'm trying to use Apache's PDFBox library. For some reason, javac is unable to load pdfbox-2.0.28.jar. When I run any of the following commands in Bash:

javac -cp "pdfbox-2.0.28.jar" MyPdf.java
javac -cp ".:pdfbox-2.0.28.jar" MyPdf.java
javac -cp "pdfbox-2.0.28.jar:." MyPdf.java
javac -sourcepath "pdfbox-2.0.28.jar" MyPdf.java

I get this error:

MyPdf.java:1: error: package org.apache.pdfbox does not exist
import org.apache.pdfbox.*;
^
1 error

I know that the jar file is not corrupted because I can extract its contents without a problem. The class files within that jar files also exist. The MyPdf.java is a simplistic java code with following content:

import org.apache.pdfbox.*;

public class MyPdf {
    public static void main (String... args) {}
}

My directory tree:

pdf
├── MyPdf.java
├── pdfbox-2.0.28.jar
└── sample.pdf

File list of pdfbox-2.0.28.jar file:

$ unzip -l pdfbox-2.0.28.jar
Archive:  pdfbox-2.0.28.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
    18161  2023-04-10 10:05   META-INF/MANIFEST.MF
        0  2023-04-10 12:05   META-INF/
     1655  2023-04-10 10:04   META-INF/DEPENDENCIES
    23477  2023-04-10 10:04   META-INF/LICENSE
      638  2023-04-10 10:04   META-INF/NOTICE
        0  2023-04-10 12:05   META-INF/maven/
        0  2023-04-10 12:05   META-INF/maven/org.apache.pdfbox/
        0  2023-04-10 12:05   META-INF/maven/org.apache.pdfbox/pdfbox/
      134  2023-04-10 10:05   META-INF/maven/org.apache.pdfbox/pdfbox/pom.properties
    43315  2023-04-10 10:04   META-INF/maven/org.apache.pdfbox/pdfbox/pom.xml
        0  2023-04-10 12:05   org/
        0  2023-04-10 12:05   org/apache/
        0  2023-04-10 12:05   org/apache/pdfbox/
        0  2023-04-10 12:05   org/apache/pdfbox/contentstream/
      431  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDContentStream.class
     7327  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDFGraphicsStreamEngine.class
    25077  2023-04-10 10:04   org/apache/pdfbox/contentstream/PDFStreamEngine.class
        0  2023-04-10 12:05   org/apache/pdfbox/contentstream/operator/
     ... snipped ...
     1514  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/ByteTrie$ByteTrieNode.class
     2585  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/ByteTrie.class
     1880  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/FileType.class
     3308  2023-04-10 10:04   org/apache/pdfbox/util/filetypedetector/FileTypeDetector.class
---------                     -------
  5603142                     879 files

I looked at some of the search results from this web search query, including the one here but none of them solved my problem.

How do I figure out what I'm doing wrong? It is not the first time for me to load a library using classpath but I cannot get this thing to work.

答案1

得分: 1

org.apache.pdfbox.* 不渲染到任何类,因为它在您的JAR文件内部不包含org/apache/pdfbox目录中的任何类。您应该导入像org.apache.pdfbox.contentstream.operator.* 这样包含实际类的内容。尝试一下。

英文:

org.apache.pdfbox.* doesn't render to any classes, because it doesn't contain any classes in org/apache/pdfbox dir inside your jar. You should import something like org.apache.pdfbox.contentstream.operator.* — anything with actual classes inside. Try it.

huangapple
  • 本文由 发表于 2023年5月28日 21:54:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定