1

I am attempting to run the following Java code on an Ubuntu system. The code should create a blank PDF file using the pdfbox class:

import org.apache.pdfbox.pdmodel.*;
import java.io.*;

public class BlankPDF {
    public static void main(String[] args) {
        PDDocument doc = null;
        try{
            doc = new PDDocument();
        } catch (IOException ie){
            System.out.println(ie);
        }
        doc.addPage(new PDPage());
        try{
            doc.save("Empty PDF.pdf");
            doc.close();
        } catch (Exception io){
            System.out.println(io);
        }
    }
}

I have the following class dependencies in the same directory as the script:

  • pdfbox-1.7.0.jar
  • jempbox-1.7.0.jar
  • fontbox-1.7.0.jar
  • commons-logging-1.1.1.jar

I used the following command to compile the script:

sudo javac BlankPDF.java -classpath pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar

Which returned no output and created a .class file (indicating that the compilation worked correctly?)

But when I attempt to run the code using the following command:

sudo java BlankPDF -classpath pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
    at BlankPDF.main(BlankPDF.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    ... 1 more

What am I missing?

2 Answers 2

4

The name of the class must be the last argument to java. The flags must precede it. If you put flags at the end of the command lines, as here, they are ignored. So:

java -classpath .:pdfbox-1.7.0.jar:fontbox-1.7.0.jar:jempbox-1.7.0.jar:commons-logging-1.1.1.jar BlankPDF
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that worked! When looking online for examples the -classpath was always at the start, but I subconsciously moved it to the end like I am use to with traditional flags
2

With ref to above answer,don't forget to add current directory (with dot sign) as well in command with jars in classpath

java -classpath hello.jar:. SampleProgram

1 Comment

+1 Good catch, I didn't notice that -- that would be the second problem he'd have!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.