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?
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: