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

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

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中运行以下任何一个命令时:

  1. javac -cp "pdfbox-2.0.28.jar" MyPdf.java
  2. javac -cp ".:pdfbox-2.0.28.jar" MyPdf.java
  3. javac -cp "pdfbox-2.0.28.jar:." MyPdf.java
  4. javac -sourcepath "pdfbox-2.0.28.jar" MyPdf.java

我会得到这个错误:

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

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

  1. import org.apache.pdfbox.*;
  2. public class MyPdf {
  3. public static void main (String... args) {}
  4. }

我的目录结构如下:

  1. pdf
  2. ├── MyPdf.java
  3. ├── pdfbox-2.0.28.jar
  4. └── sample.pdf

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

  1. $ unzip -l pdfbox-2.0.28.jar
  2. Archive: pdfbox-2.0.28.jar
  3. Length Date Time Name
  4. --------- ---------- ----- ----
  5. 18161 2023-04-10 10:05 META-INF/MANIFEST.MF
  6. 0 2023-04-10 12:05 META-INF/
  7. 1655 2023-04-10 10:04 META-INF/DEPENDENCIES
  8. 23477 2023-04-10 10:04 META-INF/LICENSE
  9. 638 2023-04-10 10:04 META-INF/NOTICE
  10. 0 2023-04-10 12:05 META-INF/maven/
  11. 0 2023-04-10 12:05 META-INF/maven/org.apache.pdfbox/
  12. 0 2023-04-10 12:05 META-INF/maven/org.apache.pdfbox/pdfbox/
  13. 134 2023-04-10 10:05 META-INF/maven/org.apache.pdfbox/pdfbox/pom.properties
  14. 43315 2023-04-10 10:04 META-INF/maven/org.apache.pdfbox/pdfbox/pom.xml
  15. 0 2023-04-10 12:05 org/
  16. 0 2023-04-10 12:05 org/apache/
  17. 0 2023-04-10 12:05 org/apache/pdfbox/
  18. 0 2023-04-10 12:05 org/apache/pdfbox/contentstream/
  19. 431 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDContentStream.class
  20. 7327 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDFGraphicsStreamEngine.class
  21. 25077 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDFStreamEngine.class
  22. 0 2023-04-10 12:05 org/apache/pdfbox/contentstream/operator/
  23. ... 省略部分 ...
  24. 1514 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/ByteTrie$ByteTrieNode.class
  25. 2585 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/ByteTrie.class
  26. 1880 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/FileType.class
  27. 3308 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/FileTypeDetector.class
  28. --------- -------
  29. 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:

  1. javac -cp "pdfbox-2.0.28.jar" MyPdf.java
  2. javac -cp ".:pdfbox-2.0.28.jar" MyPdf.java
  3. javac -cp "pdfbox-2.0.28.jar:." MyPdf.java
  4. javac -sourcepath "pdfbox-2.0.28.jar" MyPdf.java

I get this error:

  1. MyPdf.java:1: error: package org.apache.pdfbox does not exist
  2. import org.apache.pdfbox.*;
  3. ^
  4. 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:

  1. import org.apache.pdfbox.*;
  2. public class MyPdf {
  3. public static void main (String... args) {}
  4. }

My directory tree:

  1. pdf
  2. ├── MyPdf.java
  3. ├── pdfbox-2.0.28.jar
  4. └── sample.pdf

File list of pdfbox-2.0.28.jar file:

  1. $ unzip -l pdfbox-2.0.28.jar
  2. Archive: pdfbox-2.0.28.jar
  3. Length Date Time Name
  4. --------- ---------- ----- ----
  5. 18161 2023-04-10 10:05 META-INF/MANIFEST.MF
  6. 0 2023-04-10 12:05 META-INF/
  7. 1655 2023-04-10 10:04 META-INF/DEPENDENCIES
  8. 23477 2023-04-10 10:04 META-INF/LICENSE
  9. 638 2023-04-10 10:04 META-INF/NOTICE
  10. 0 2023-04-10 12:05 META-INF/maven/
  11. 0 2023-04-10 12:05 META-INF/maven/org.apache.pdfbox/
  12. 0 2023-04-10 12:05 META-INF/maven/org.apache.pdfbox/pdfbox/
  13. 134 2023-04-10 10:05 META-INF/maven/org.apache.pdfbox/pdfbox/pom.properties
  14. 43315 2023-04-10 10:04 META-INF/maven/org.apache.pdfbox/pdfbox/pom.xml
  15. 0 2023-04-10 12:05 org/
  16. 0 2023-04-10 12:05 org/apache/
  17. 0 2023-04-10 12:05 org/apache/pdfbox/
  18. 0 2023-04-10 12:05 org/apache/pdfbox/contentstream/
  19. 431 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDContentStream.class
  20. 7327 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDFGraphicsStreamEngine.class
  21. 25077 2023-04-10 10:04 org/apache/pdfbox/contentstream/PDFStreamEngine.class
  22. 0 2023-04-10 12:05 org/apache/pdfbox/contentstream/operator/
  23. ... snipped ...
  24. 1514 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/ByteTrie$ByteTrieNode.class
  25. 2585 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/ByteTrie.class
  26. 1880 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/FileType.class
  27. 3308 2023-04-10 10:04 org/apache/pdfbox/util/filetypedetector/FileTypeDetector.class
  28. --------- -------
  29. 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:

确定