【问题标题】:Java - pdfbox cannot import jar?Java - pdfbox 无法导入 jar?
【发布时间】:2011-11-18 03:36:21
【问题描述】:

嗨,请有人帮我解决这个简单问题,我相信...我已经在 java 聊天网站上询问了 8 位专家,但似乎没有人能帮助我:( . 我已经从 http://pdfbox.apache.org/download.html。 我打开了 blueJ IDE 并加载了罐子。当我输入时

import org.apache.pdfbox.*; 
import org.apache.pdfbox.pdmodel; 
import org.apache.pdfbox.pdmodel.PDPage; 

我收到一条错误消息:

error has occured cannot find org.apache.pdfbox

我也尝试过 netbeans 并转到项目属性并添加了 jar,我也转到了 netbeans 的侧面菜单并尝试了这种方式。我仍然得到同样的错误。有人可以帮忙吗?我已经在 3 台不同的电脑上试过了。

好的,伙计们给我更多信息。我下载了 jar 并将它们放在 blueJ 中的一个文件夹中,我转到选项并选择了他们说“已加载”的 jar 文件。我在 Netbeans 中也做了同样的事情,我已经向 IDE 展示了它仍然无法工作的 Jars 这里是完整的代码,它只是从我正在尝试的 PDFBOX 网站获取的示例代码。

import org.apache.pdfbox.exceptions.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

/**
 * This will create a blank PDF and write the contents to a file.
  */
public class CreateBlankPDF
{

/**
 * This will create a blank PDF and write the contents to a file.
 *
 * @param file The name of the file to write to.
 *
 * @throws IOException If there is an error writing the data.
 * @throws COSVisitorException If there is an error while generating the document.
 */
public void create( String file ) throws IOException, COSVisitorException
{
    PDDocument document = null;
    try
    {
        document = new PDDocument();
        //Every document requires at least one page, so we will add one
        //blank page.
        PDPage blankPage = new PDPage();
        document.addPage( blankPage );
        document.save( file );
    }
    finally
    {
        if( document != null )
        {
            document.close();
        }
    }
}

/**
 * This will create a blank document.
 *
 * @param args The command line arguments.
 *
 * @throws IOException If there is an error writing the document data.
 * @throws COSVisitorException If there is an error generating the data.
 */
public static void main( String[] args ) throws IOException, COSVisitorException
{
    if( args.length != 1 )
    {
        usage();
    }
    else
    {
        CreateBlankPDF creator = new CreateBlankPDF();
        creator.create( args[0] );
    }
}

/**
 * This will print the usage of this class.
 */
private static void usage()
{
    System.err.println( "usage: java org.apache.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf>" );
}

}

【问题讨论】:

  • 当你说你“去项目属性并添加了JAR”时,你的意思是你已经将它添加到构建路径中了吗?
  • 是的,我已将其添加到 Netbeans 的编译时库中

标签: java pdf netbeans pdfbox bluej


【解决方案1】:

这是排序的。我下载的 JAR 文件错误。我检查了文件大小,发现它本来应该超过 9mb,但它只有 20kb。谢谢大家!

【讨论】:

    【解决方案2】:

    下载这些 jar 文件后,您对它们做了什么?您是如何将它们添加到您的项目中的? Netbeans 无法猜测 jars 在您计算机上的位置,这就是为什么在您导入时它不起作用的原因......将 jars 添加到您的 Netbeans 项目中:

    假设 jar 文件位于 c:\downloads

    在 netbeans 中选择项目后,转到 Properties->sources 并选择 Compile Tab,然后导航到 jars 所在的位置并添加它们。现在应该清除您的导入错误。

    【讨论】:

    • 而且不要使用 blueJ,这很糟糕。为 Java SE 开发人员试用 Eclipse:
    • +1 用于 Eclipse。 Eclipse 是我的首选 IDE,我讨厌 blueJ。
    • 我实际上是在不久前开始使用 blueJ 开发的(我什至不知道它仍然存在 :))。还不错,我的意思是对于一个带有 35463 个按钮的大 Eclipse 来说可能会令人生畏......这就是为什么我建议初学者使用 Eclipse for Java SE(不是 JAva EE 的那个)
    【解决方案3】:

    我找不到这个“Pdfbox”产品的Javadocs,但我确实找到了一些示例代码,而且似乎没有一个使用org.apache.pdfbox 中的任何类,而是像org.apache.pdfbox.pdmodel 这样的子包。现在,知道了这一点,我可以在您的导入语句中看到两件事:如果org.apache.pdfbox 中实际上没有类并且您不需要导入该包,则第一行将给出您显示的错误;第二行将给出错误,因为`org.apache.pdfbox.pdmodel 本身就是一个包,但您尝试将它作为一个类导入。我确信这两个问题之一(或两者兼有)是您的实际问题。

    【讨论】:

    • 是否可以再次编写导入语句我确实从 pdfbox.pdmodel 更改了它,因为它最初不起作用我认为导入整个包会更容易
    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多