1

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.

user47
  • 1,080
  • 11
  • 28

1 Answers1

1

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.

Chus
  • 146
  • 7
  • javac does compile correctly when I followed your instructions. I was under the impression that `.*` works irrespective of if a class exists within the root directory of the package of not. – user47 May 28 '23 at 14:32
  • If there are no compilable classes within a folder, then it isn't recognised as a java package. And you can't import anything that is not a package. Otherwise you would be able to do something like "import org.*" or even "import *". Which doesn't make any sense of course. – Chus May 28 '23 at 15:14
  • @user47 - Your impression is incorrect. See https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html ... and read the section entitled "Apparent Hierarchies of Packages". – Stephen C May 28 '23 at 15:22
  • And read https://stackoverflow.com/a/15857488/139985 for more explanation. – Stephen C May 28 '23 at 15:27