我下载itext-1.3并放入lib文件夹中jdk 1.6.0.并在系统变量中添加了lib文件夹作为CLASSPATH.
但是当我运行程序时,我收到错误:
包com.itextpdf.text不存在.
同样适用于所有其他包.我犯了什么错误?
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorld {
/** Path to the resulting PDF file. */
public static final String RESULT
= "E:/hello.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new HelloWorld().createPdf(RESULT);
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
}
Run Code Online (Sandbox Code Playgroud)
除非您明确指示,否则不应向JVM/JDK lib或ext文件夹添加任何内容
根据您的开发环境(以及您未来的意图),您应该将库放在最适合它的位置,例如,在项目文件夹中的lib目录中(但在源代码之外).
您应该将类路径依赖项添加到项目Jar清单中(请查看向JAR文件的类路径添加类)或使用-cp命令行上的参数来执行程序.您应该使用javac的-classpath选项来编译程序
至于开发环境,这取决于您使用的是什么
特别说明
每个Jar文件都需要在Classpath上单独引用,不能指定文件夹并期望JVM扫描其内容以获取Jar文件,仅适用于类
更新了编译执行示例
我下载iText 5.3.1.从我提取的zip文件:
并将它们放置在易于到达的位置.
我从iText in Action网站下载了HelloWorld示例.我将它放在src与Jar相同位置的目录中
我修改了代码,以便在当前工作目录中创建生成的PDF
public static final String RESULT = "hello.pdf";
Run Code Online (Sandbox Code Playgroud)
我用javac.exe -cp d:\hold\itextpdf-5.3.1.jar;d:\hold\itext-pdfa-5.3.1.jar;d:\hold\itext-xtra-5.3.1.jar -d . src\HelloWorld.java(编译d:\hold)编译了这个例子
这在part1\chapter01中创建了HelloWorld类 D:\hold
然后我用这个例子执行了 java -cp d:\hold\itextpdf-5.3.1.jar;d:\hold\itext-pdfa-5.3.1.jar;d:\hold\itext-xtra-5.3.1.jar;d:\hold part1.chapter01.HelloWorld
这导致在hello.pdf当前目录(D:\hold)中创建
| 归档时间: |
|
| 查看次数: |
14352 次 |
| 最近记录: |