所以我从https://github.com/itext/itext7/releases/latest 并将它们放在与itext示例c01e01\u helloworld.java相同的文件夹中,但当我运行
javac C01E01_HelloWorld.java
我明白了
$ javac C01E01_HelloWorld.java
C01E01_HelloWorld.java:3: error: package com.itextpdf.kernel.pdf does not exist
import com.itextpdf.kernel.pdf.PdfDocument;
^
C01E01_HelloWorld.java:4: error: package com.itextpdf.kernel.pdf does not exist
import com.itextpdf.kernel.pdf.PdfWriter;
^
C01E01_HelloWorld.java:5: error: package com.itextpdf.layout does not exist
import com.itextpdf.layout.Document;
^
C01E01_HelloWorld.java:6: error: package com.itextpdf.layout.element does not exist
import com.itextpdf.layout.element.Paragraph;
^
C01E01_HelloWorld.java:25: error: cannot find symbol
PdfWriter writer = new PdfWriter(dest);
^
symbol: class PdfWriter
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:25: error: cannot find symbol
PdfWriter writer = new PdfWriter(dest);
^
symbol: class PdfWriter
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:28: error: cannot find symbol
PdfDocument pdf = new PdfDocument(writer);
^
symbol: class PdfDocument
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:28: error: cannot find symbol
PdfDocument pdf = new PdfDocument(writer);
^
symbol: class PdfDocument
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:31: error: cannot find symbol
Document document = new Document(pdf);
^
symbol: class Document
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:31: error: cannot find symbol
Document document = new Document(pdf);
^
symbol: class Document
location: class C01E01_HelloWorld
C01E01_HelloWorld.java:34: error: cannot find symbol
document.add(new Paragraph("Hello World!"));
^
symbol: class Paragraph
location: class C01E01_HelloWorld
11 errors
我也试过了
javac -cp /home/user01/itext/demo/ C01E01_HelloWorld.java
i、 其中-cp指向itext jar(和c01e01\u helloworld.java)具有相同结果的位置。如何让导入行知道并使用itext jar文件?
或者这不能用一种简单的方法来完成,并且需要eclipse、maven或其他工具吗?
因此,我运行以下没有错误
javac -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld.java
如果我遗漏了任何jar文件,就会导致与丢失的jar相关的错误。但当我尝试
java C01E01_HelloWorld
我明白了
Error: Unable to initialize main class C01E01_HelloWorld
Caused by: java.lang.NoClassDefFoundError: com/itextpdf/layout/element/IBlockElement
我也试过了
java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar C01E01_HelloWorld
但结果略有不同
Error: Could not find or load main class C01E01_HelloWorld
Caused by: java.lang.ClassNotFoundException: C01E01_HelloWorld
所以下一步就是
$ java -cp kernel-7.1.13.jar:layout-7.1.13.jar:io-7.1.13.jar:. C01E01_HelloWorld
Exception in thread "main" java.lang.NullPointerException at C01E01_HelloWorld.main(C01E01_HelloWorld.java:21)
我的消息来源是
/*package tutorial.chapter01;*/
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;
/**
* Simple Hello World example.
*/
public class C01E01_HelloWorld {
/* public static final String DEST = "results/chapter01/hello_world.pdf"; */
public static final String DEST = "hello_world.pdf";
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C01E01_HelloWorld().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
//Add paragraph to the document
document.add(new Paragraph("Hello World!"));
//Close document
document.close();
}
}
2条答案
按热度按时间5jdjgkvh1#
安装一个像样的ide,比如intellijidea社区版。
克隆包含以下示例的github存储库:
在intellij中,打开
pom.xml
在刚才克隆的repo目录中。intellij会询问您是否要将其作为maven项目打开。在intellij的左侧,您有一个项目中所有文件的树视图。转到文件
src/main/java/tutorial/chapter01/C01E01_HelloWorld.java
.运行示例
main
方法)。6tdlim6h2#
所以我使用中提供的链接下载了slf4j.api、slf4j-log4j12和log4j
slf4j:itext7到底需要哪些.jar文件?
还下载了
https://github.com/itext/itext7/releases/latest
然后我跑了
然后
最后我得到了所谓的简单你好世界pdf。。。。
我试过maven、eclipse和now idea,但都没能“仅仅”按照教程或提示工作。。。。然而