java.lang.noClassDefFoundError : com/itextpdf/text/element on a Java Applet

标签 java applet itext noclassdeffounderror

我正在尝试编写一个 Word-To-PDF 转换器的代码,并将 Java 代码以 Java Applet 的形式构建到 HTML 网站中。

我使用了 http://java.worldbestlearningcenter.com/2013/07/word-to-pdf-converter.html 中的 Java 代码。代码本身似乎工作得很好,当我在 Eclipse 中运行该程序时,我确实可以将 Word 文档转换为 PDF。现在我想将该代码添加到我的 HTML 文件中并使其正常工作。

为此,我让我的类继承 JApplet 并编写了 Paint() 方法。 我现在的代码如下所示:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.PageSize;
import java.applet.*;
import java.awt.*;

public class ConverterApplet extends Applet {
    private static final long serialVersionUID = 1L;

    public void paint(Graphics g) {
        g.drawString("Hello", 60, 40);
        selectFiles();
    }

    public static void selectFiles(){
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Microsoft Word 2007+", "docx");
        chooser.setFileFilter(filter);
        chooser.setMultiSelectionEnabled(true);
        int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File[] Files=chooser.getSelectedFiles();
            System.out.println("Konvertierung gestartet ...");
            for( int i=0;i<Files.length;i++){     
                String wordfile=Files[i].toString();            
                convertWordToPdf(wordfile,wordfile.substring(0,wordfile.indexOf('.'))+".pdf");
            }

            System.out.println("Konvertierung abgeschlossen!");
        }
    }

    public static void convertWordToPdf(String src, String desc){
        try {
            //create file inputstream object to read data from file 
            FileInputStream fs=new FileInputStream(src);
            //create document object to wrap the file inputstream object
            XWPFDocument doc=new XWPFDocument(fs); 
            //72 units=1 inch
            Document pdfdoc=new Document(PageSize.A4,72,72,72,72);
            //create a pdf writer object to write text to mypdf.pdf file
            PdfWriter pwriter=PdfWriter.getInstance(pdfdoc, new FileOutputStream(desc));
            //specify the vertical space between the lines of text
            pwriter.setInitialLeading(20);
            //get all paragraphs from word docx
            List<XWPFParagraph> plist=doc.getParagraphs();

            //open pdf document for writing
            pdfdoc.open();
            for (int i = 0; i < plist.size(); i++) {
                //read through the list of paragraphs
                XWPFParagraph pa = plist.get(i);
                //get all run objects from each paragraph
                List<XWPFRun> runs = pa.getRuns();
                //read through the run objects
                for (int j = 0; j < runs.size(); j++) {       
                    XWPFRun run=runs.get(j);
                    //get pictures from the run and add them to the pdf document
                    List<XWPFPicture> piclist=run.getEmbeddedPictures();
                    //traverse through the list and write each image to a file
                    Iterator<XWPFPicture> iterator=piclist.iterator();
                    while(iterator.hasNext()){
                        XWPFPicture pic=iterator.next();
                        XWPFPictureData picdata=pic.getPictureData();
                        byte[] bytepic=picdata.getData(); 
                        Image imag=Image.getInstance(bytepic);
                        pdfdoc.add(imag);

                    }
                    //get color code
                    int color=getCode(run.getColor());
                    //construct font object
                    Font f=null;
                    if(run.isBold() && run.isItalic())
                        f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLDITALIC, new BaseColor(color));
                    else if(run.isBold())
                        f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLD, new BaseColor(color));
                    else if(run.isItalic())
                        f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.ITALIC, new BaseColor(color));
                    else if(run.isStrike())
                        f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.STRIKETHRU, new BaseColor(color));
                    else
                        f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.NORMAL, new BaseColor(color));
                    //construct unicode string
                    String text=run.getText(-1);
                    byte[] bs;
                    if (text!=null){
                        bs=text.getBytes();
                        String str=new String(bs,"UTF-8");
                        //add string to the pdf document
                        Chunk chObj1=new Chunk(str,f);
                        pdfdoc.add(chObj1);
                    }      

                }
                //output new line
                pdfdoc.add(new Chunk(Chunk.NEWLINE));
            }
            //close pdf document  
            pdfdoc.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    } 

    public static int getCode(String code){
        int colorCode;
        if(code!=null)
            colorCode=Long.decode("0x"+code).intValue();
        else
            colorCode=Long.decode("0x000000").intValue();
        return colorCode;
    }
}

我使用以下代码创建了一个 HTML 文档:

<DOCTYPE! html>
<html lang="DE-CH">
    <head>
        <title>Testseite</title>
    </head>

    <body>
        <h1>Titel</h1>

        <object type="application/x-java-applet" width="800" height="600">
            <param name="code" value="ConverterApplet" />
            <param name="archive" value="ConverterApplet.jar" />
            Applet failed to run.  No Java plug-in was found.
        </object>

    </body>
</html> 

当尝试在 Internet Explorer 中打开该 HTML 页面时(Chrome 和 Firefox 似乎无法工作,甚至 IE 也需要特殊权限),我收到错误“java.lang.noClassDefFoundError: com/itextpdf/text/element”。

我还可以告诉你:

  • 我只有一个 itextpdf 的 JAR 文件。它是版本 5.1.0,因为 2.1.7 不包含特定代码所需的所有包。没有其他类似于 itextpdf 的 JAR 文件。
  • 我的项目文件夹中的 WEB-INF/lib 文件夹中包含所有 JAR 文件
  • lib 文件夹内的所有 JAR 文件也位于 CLASSPATH 中。
  • 我将代码的更新版本导出为“ConverterApplet.jar”,该文件也位于项目的根文件夹和 CLASSPATH 中。
  • 我尝试了 Chrome、Firefox、Opera、Edge 和 Internet Explorer 等浏览器。只有最后一个甚至让我有可能看到(不工作的)小程序,所有其他人都说“没有找到插件”。

除了上面列出的要点之外,我在网络上找不到任何其他帮助。我真的不知道还有什么问题会导致这样的错误,特别是因为 Java 代码本身可以在 Eclipse 内部运行,但不能在外部运行。任何帮助都会非常感激。

== 编辑 == .classpath 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/ConverterApplet.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/dom4j-1.6.1.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/openxml4j-1.0-beta.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-3.9.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-ooxml-3.8.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/poi-ooxml-schemas-3.9.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/xmlbeans-2.5.0.jar"/>
    <classpathentry kind="lib" path="C:/Users/silve/Documents/Eclipse_Launch/Converter/itextpdf-5.5.12.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

新的 HTML 文件:

<DOCTYPE! html>
<html lang="DE-CH">
    <head>
        <title>Testseite</title>
    </head>

    <body>
        <h1>Titel</h1>

        <object type="application/x-java-applet" width="800" height="600">
            <param name="code" value="ConverterApplet" />
            <param name="archive" value="~/ConverterApplet.jar" />
            <param name="dom_archive" value="~/dom4j-1.6.1.jar" />
            <param name="itext_archive" value="~/itextpdf-5.5.12.jar" />
            <param name="openxml_archive" value="~/openxml4j-1.0-beta.jar" />
            <param name="poi_archive" value="~/poi-3.9.jar" />
            <param name="ooxml_archive" value="~/poi-ooxml-3.8.jar" />
            <param name="schemas_archive" value="~/poi-ooxml-schemas-3.9.jar" />
            <param name="mxmlbeans_archive" value="~/xmlbeans-2.5.0" />

            Applet failed to run.  No Java plug-in was found.
        </object>

    </body>
</html> 

现在我收到 IE 错误“NoClassFound”。

最佳答案

I have all JAR Files in the WEB-INF/lib folder within the project's folder

浏览器无法直接获取该位置的 Jar(尝试将 Jar 的完整路径放入浏览器地址栏中并按 Enter 键),或者通过扩展 JVM。

All JAR Files inside the lib folder are also in CLASSPATH.

CLASSPATH到底是什么? ?

但这些并不是唯一的问题。

<param name="archive" value="ConverterApplet.jar" />

iText jar(以及与小程序相关的每个 jar)需要在 archive 中明确引用元素。

I'm really clueless what else could be the problem to get such an error, especially because the Java code itself works inside Eclipse, but not outside.

编写一个小应用程序并将其部署到 Internet 上进行一般使用,是大约具有同等难度的问题。或者换句话说,当“在 IDE 中运行正常,只需部署它”时,工作就完成了一半。

编辑

.classpath is the file in my Main root of the project

该文件仅对您的 IDE 有用。它不会被浏览器(或使用浏览器启动小程序时的 JVM)访问或占用。

<object type="application/x-java-applet" width="800" height="600">
        <param name="code" value="ConverterApplet" />
        <param name="archive" value="~/ConverterApplet.jar" />
        <param name="dom_archive" value="~/dom4j-1.6.1.jar" />
        ....

我对 object 有点生疏了元素,但是 AFAIR,archive param用作 archive applet 的属性将使用元素。

因此下一行就是开始出错的地方。您的小程序本身可能会读取 dom_archive属性,但 JM 不会考虑该存档位于运行时类路径上。对于这两行,它应该是:

<object type="application/x-java-applet" width="800" height="600">
        <param name="code" value="ConverterApplet" />
        <param name="archive" value="~/ConverterApplet.jar ~/dom4j-1.6.1.jar .." />
        ....

在(单个) archive 中以空格分隔的列表中命名每个 Jar元素。然后它们将被放在小程序的运行时类路径中。

关于java.lang.noClassDefFoundError : com/itextpdf/text/element on a Java Applet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46485826/

相关文章:

css - 如何将自定义颜色应用于 PdfPCell 的背景?

Java 准备好的语句未输入数据并命中捕获

java - HashSet 插入 2 个相等的元素

java - 一个关于ubuntu中java和环境变量的简单问题

java - 在哪里可以找到需要 Java 6 的小程序?

android - 在 Android 上使用 iText 生成波斯语和英语 PDF

java - 在 JPanel 上添加 JLabel - BorderLayout

在 Windows 和 Linux 中使用 USB 访问的 Java 小程序

java - java 小程序错误

c# - 将 Pdf 列表中的某些文本加粗