java itext 包中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11814375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:27:24  来源:igfitidea点击:

Error in itext package

javaswingitext

提问by Gomathi

I downloaded itext-1.3and placed into lib folder of jdk 1.6.0. And added the lib folder as the CLASSPATH in system variables.

我下载itext-1.3并放入 .lib 文件夹中jdk 1.6.0。并在系统变量中添加了 lib 文件夹作为 CLASSPATH。

But while I run the program I get the error:

但是当我运行程序时出现错误:

package com.itextpdf.text does not exist.

包 com.itextpdf.text 不存在。

Similarly for all other packages too. What mistake I made?

对于所有其他包也是如此。我犯了什么错误?

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();
    }
}

回答by MadProgrammer

You shouldn't add anything to the JVM/JDK lib or ext folders unless you instructed to do so explicitly

除非明确指示,否则不应向 JVM/JDK lib 或 ext 文件夹添加任何内容

Depending on you development environment (& your future intentions), you should place the libraries in a location best suited to it, for example, in a lib directory within you project folder (but outside of the source).

根据您的开发环境(以及您的未来意图),您应该将库放在最适合它的位置,例如,在项目文件夹内(但在源之外)的 lib 目录中。

You should either add a class-path dependence into the projects Jar manifest (check out Adding Classes to JAR File's Classpath) or use the -cpparameter on the command line to execute the program. You should use the -classpath option of the javac to compile the program

您应该将类​​路径依赖项添加到项目 Jar 清单中(查看将类添加到 JAR 文件的类路径)或使用-cp命令行上的参数来执行程序。您应该使用 javac 的 -classpath 选项来编译程序

As for the development environment, that depends on what you are using

至于开发环境,那要看你用的是什么

Special note

特别说明

Each Jar file needs to be referenced separately on the Classpath, you can not specify a folder & expect the JVM to scan its contents for Jar files, only works for classes

每个 Jar 文件都需要在 Classpath 上单独引用,您不能指定文件夹并期望 JVM 扫描其内容以查找 Jar 文件,仅适用于类

UPDATED WITH COMPILE EXECUTE EXAMPLE

使用编译执行示例更新

I download iText 5.3.1. From the zip file I extracted:

我下载了 iText 5.3.1。从我提取的 zip 文件中:

  • itextpdf-5.3.1.jar
  • itext-pdfa-5.3.1.jar
  • itext-xtra-5.3.1.jar
  • itextpdf-5.3.1.jar
  • itext-pdfa-5.3.1.jar
  • itext-xtra-5.3.1.jar

And placed them in an easy to reach location.

并将它们放置在易于到达的位置。

I downloaded the HelloWorld examplefrom the iText in Action website. I placed this in the srcdirectory under the same location as the Jar's

我从 iText in Action 网站下载了HelloWorld 示例。我把它放在src与 Jar 相同位置的目录中

I modified the code so that the resulting PDF would be created in the current working directory

我修改了代码,以便在当前工作目录中创建生成的 PDF

public static final String RESULT = "hello.pdf";

I compiled the example with 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(compiled in d:\hold)

我用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)编译了这个例子

This created the HelloWorld class in part1\chapter01 in D:\hold

这在 part1\chapter01 中创建了 HelloWorld 类 D:\hold

I then executed the example with 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

然后我执行了这个例子 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

This resulted in the creation of hello.pdfin the current directory (D:\hold)

这导致在hello.pdf当前目录中创建( D:\hold)

回答by Jay

Don't need to do anything on jdk library. Just need to give path of itext jar files, to the java by using classpath.

不需要在 jdk 库上做任何事情。只需要通过使用类路径将itext jar文件的路径提供给java。

I also suffer from the same problem but having itext 5.5.3. In my case I create a libfolder in parallel to my working folder. In libfolder I put all the three jar files (itextpdf-5.5.3.jar, itext-pdfa-5.5.3.jar, itext-xtra-5.5.3.jar).Don't waste time in giving the path of each jar file separately at the time of compiling and running the code.

我也遇到同样的问题,但有 itext 5.5.3。在我的情况下,我创建了一个与我的工作文件夹并行的lib文件夹。在lib文件夹中,我放置了所有三个 jar 文件(itextpdf-5.5.3.jar、itext-pdfa-5.5.3.jar、itext-xtra-5.5.3.jar)。不要浪费时间在编译和运行代码的时候分别给出每个jar文件的路径。

I compiled the code, eg1.java(c:\ItextPractise\code\eg1.java) with

我编译了代码 eg1.java(c:\ItextPractise\code\eg1.java)

javac -classpath c:\ItextPractise\lib\*;. eg1.java

javac -classpath c:\ItextPractise\lib\*;. eg1.java

and run using

并使用

java -classpath c:\ItextPractise\lib\*;. eg1(eg1 is the name of main class in my case)

java -classpath c:\ItextPractise\lib\*;. eg1(在我的例子中,eg1 是主类的名称)

You may know more about classpath from OracleOfficialPage.

您可能从OracleOfficialPage了解更多关于 classpath 的信息

回答by shehu abdul

this might be as a result of the absence of the itext library in your project. simply download the itext jar file and add it to your project library

这可能是因为您的项目中没有 itext 库。只需下载 itext jar 文件并将其添加到您的项目库中