Question

I have a java jar file i got below error when executing in ubuntu terminal .

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
    at com.example.pdfbox.pdfbox.main(pdfbox.java:41)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

and my code is like:

package com.example.pdfbox;

import java.io.BufferedWriter;
import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.util.PDFTextStripper;

public class pdfbox {

public static void main(String[] args) {
    PDDocument pd;
    BufferedWriter wr;


    String kunde = args[0];
    String sted = args[1];
    String plassering = args[2];



    try {
        File input = new File("/home/processdrive/Desktop/SKI108RE.PDF");

         pd = PDDocument.load(input);// line number 41
         System.out.println(pd.getNumberOfPages());
         System.out.println(pd.isEncrypted());

         PDDocumentCatalog catalog = pd.getDocumentCatalog();

         PDAcroForm form = catalog.getAcroForm();

         form.getField("Kunde").setValue(kunde);
         form.getField("Sted").setValue(sted);
         form.getField("Plassering").setValue(plassering);


         pd.save("/home/processdrive/Desktop/CopyOfInvoice.pdf");
         PDFTextStripper stripper = new PDFTextStripper();
         stripper.setStartPage(1); 
         stripper.setEndPage(1); 

 } catch (Exception e){
         e.printStackTrace();
         System.out.println("Error occured");
        } 
     }
}

The 41 line is

pd = PDDocument.load(input);

My calss path is like

<classpath><classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
    <classpathentry exported="true" kind="lib" path="/home/processdrive/Desktop/pdfbox/pdfbox-1.8.4.jar"/>
    <classpathentry exported="true" kind="lib" path="/home/processdrive/Desktop/pdfbox/commons-logging-1.1.1.jar"/>
    <classpathentry exported="true" kind="lib" path="/home/processdrive/Desktop/pdfbox/fontbox-1.8.4.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
Was it helpful?

Solution

Well, the answer depends on how you are going to create your runnable JAR file. As you are using eclipse, I assume you simply exported your project.

First ... When exporting, always select Runnable JAR file as the export type when having a - hmm - runnable program (with a main class), as this will create the manifest file with all appropriate entries. The most important entries are - by the way - the Main-Class and the Class-Path entry.

The export wizard has a handful of options with which you can control what and how is really exported. This will also have an effect on the manifest file, of course. Look at this screenshot:

enter image description here

You see the three options regarding library handling? Let me explain:

  • Extract required libraries into generated JAR

The easiest option for small programs. This will create one JAR file containing all classes (and other resources) of your libraries. In fact, the content of all libraries is extracted and put into the exported JAR. This is called a Fat JAR.

  • Package required libraries into generated JAR

This is some other form of a fat JAR. All your libraries are put as is into the generated JAR file. That is, your generated JAR contains your classes and resources and all library JAR files. Starting your own main class now doesn't work, as JARs in JARs cannot be read correctly. Therefore eclipse creates a Main-Class entry in the manifest file pointing to the class org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader. This main class knows how to load the contents of all packaged libraries, and it starts your own main class.

  • Copy required libraries into a sub-folder next to generated JAR

This is the - I would say - most professional option. This is preferrable for larger projects. The generated JAR file will now only contain your project's classes and resource. Additionally a sub-folder (next to your JAR) will be created that contains all your library JARS. The manifest file of your JAR now must contain a Class-Path entry that lists all those libraries. This is what Jon Skeet asked for in his comment. The export wizard will create this manifest entry correctly.


Regardless of what export option you select, you are then able to start your program with a simple

java -jar your-jar-file

OTHER TIPS

this is Build path problem

Right click project-->click build path-->Configure Build Path-->Order and Export--> and check weather your library is checked if not do check your library.

and run your project

Check for proper jar version. following pdfbox.jar had PDFDocument defination,

[MAVEN] pdfbox-0.6.4.jar
[MAVEN] pdfbox-0.7.1.jar
[MAVEN] pdfbox-0.7.3.jar

the jar you are using dont have needed class definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top