Daniel R. Collins
Daniel R. Collins

Reputation: 961

Problem accessing one jar file from another

I've written a Java program that uses classes from the Apache PDFBox jar application. I have my compiled classes and the PDFBox jar file in on directory. I can run this successfully:

java -cp .;pdfbox-app.jar Athena NPCGenerator -pdf

However, when I jar up my own program and try to run it in the same place in a similar fashion, this fails:

java -cp .;pdfbox-app.jar -jar Athena.jar NPCGenerator -pdf

Error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdm
odel/PDDocument
        at CharacterPDF.writePDF(CharacterPDF.java:49)
        at NPCGenerator.printToPDF(NPCGenerator.java:302)
        at NPCGenerator.makeAllNPCs(NPCGenerator.java:278)
        at NPCGenerator.main(NPCGenerator.java:316)
        at Athena.runApp(Athena.java:88)
        at Athena.main(Athena.java:104)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocumen
t
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

What do I need to do to fix this?

Upvotes: 0

Views: 32

Answers (1)

If you use the -jar flag to java it expects a executable jar. It will then ignore the classpath on the command line and read the classpath from the jars manifest.

You can either add pdfbox-app.jar to the manifest in Athena.jar together with the main-class attribute. You can even specify a relative or absolute path to the jar in the manifest.

Main-Class: Athena
Class-Path: pdfbox-app.jar

Or you can not use -jar and add Athena.jar to your classpath via the command line. In this case you need to also specify the main class on the command line as java will not read the manifest in this case.

java -cp .;pdfbox-app.jar;Athena.jar Athena NPCGenerator -pdf

Upvotes: 1

Related Questions