【问题标题】:Java application runs properly in Eclipse, but not as .jarJava 应用程序在 Eclipse 中正常运行,但不是 .jar
【发布时间】:2011-04-06 20:18:39
【问题描述】:

我正在创建一个使用 iText 创建 4 个 PDF 文件的 Java 应用程序。在创建包含图像的 PDF 的文件中,.jar 创建一个 0 字节文件并且不会继续执行。但是,当我右键单击 >> 运行方式 >> Java 应用程序时,它工作得很好。要创建 jar,我正在执行以下操作

  • 右键src
  • 导出
  • 可运行的 JAR 文件
  • 将所需的库提取到生成的 JAR 中
  • 完成

并且文件“penguin.jpg”在src目录下。

这是我的代码

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;

public class ITextHelloWorld 
{
    public ITextHelloWorld() throws Exception
    {
        // Create the new document objects
        Document helloWorld = new Document();
        Document bigHello = new Document();
        Document linux = new Document();
        Document tables = new Document();


    /**********************************************************
                        start Big Hello.pdf
    This document is a huge document of text. Approximately 
    28 million characters, 24,391 pages, and 9.5 MB.
     **********************************************************/
    PdfWriter.getInstance(bigHello, new FileOutputStream("Big Hello.pdf"));
    bigHello.open();

    for (int i=0; i <1000000; i++)
    {
        bigHello.add(new Paragraph("Hello World. It's me again."));
    }

    bigHello.close();
    /**********************************************************
                        end Big Hello.pdf
     **********************************************************/

    /**********************************************************
                        start Tables.pdf
    This document will have tables in it
     **********************************************************/

    PdfWriter.getInstance(tables, new FileOutputStream("Tables.pdf"));
    tables.open();

    PdfPTable table=new PdfPTable(4);
    for (int i = 1; i<100; i++)
    {       
        table.addCell("This is cell #" + i + ".\n");
    }

    tables.add(table);
    tables.close();
    /**********************************************************
                        end Tables.pdf
     **********************************************************/

    /**********************************************************
                        start Linux.pdf
    This is a document that has text, graphics, and links.
     **********************************************************/
    PdfWriter.getInstance(linux, new FileOutputStream("Graphics and Text.pdf"));
    linux.open();       
    Image image = Image.getInstance("penguin.jpg");
    linux.add(image);

    linux.add(new Paragraph("Let's talk about Linux. \n\n" +
            "Linux (commonly pronounced /ˈlɪnəks/ LIN-əks in American English, also pronounced " +
            "/ˈlɪnʊks/ LIN-ooks in Europe and Canada) refers to the family of Unix-like computer " +
            "operating systems using the Linux kernel. Linux can be installed on a wide variety of " +
            "computer hardware, ranging from mobile phones, tablet computers and video game consoles, " +
            "to mainframes and supercomputers. Linux is predominantly known for its use " +
            "in servers; in 2009 it held a server market share ranging between 20–40%. Most desktop " +
            "computers run either Microsoft Windows or Mac OS X, with Linux having anywhere from a " +
            "low of an estimated 1–2% of the desktop market to a high of an estimated 4.8%. " +
            "However, desktop use of Linux has become increasingly popular in recent years, partly " +
            "owing to the popular Ubuntu, Fedora, Mint, and openSUSE distributions and the emergence" +
            " of netbooks and smartphones running an embedded Linux."));

    linux.close();
    /**********************************************************
                        end Linux.pdf
     **********************************************************/

    /**********************************************************
                        start Hello World.pdf
    This document is one line of text.
     **********************************************************/
    PdfWriter.getInstance(helloWorld, new FileOutputStream("Hello World.pdf"));
    helloWorld.open();
    helloWorld.add(new Paragraph("Hello World. It's me again."));
    helloWorld.close();
    /**********************************************************
                        end Hello World.pdf
     **********************************************************/

}

public static void main (String args[])
{
    try
    {
        new ITextHelloWorld();
    }

    catch (Exception e)
    {
        System.out.println(e);
    }
}

}

感谢您的帮助!
托马斯

【问题讨论】:

  • 您在运行 JAR 时是否看到任何错误消息?
  • 不,我没有收到任何错误消息。只是没有创建文件“Graphics and Text.pdf”。
  • 我想知道是否有可能:你如何运行 jar? jar 可以像 .exe 一样在命令行上运行吗?

标签: java eclipse jar executable-jar


【解决方案1】:

Thomas,问题是当您创建 jar 时,您“搞乱”了目录结构。您需要使用以下方法从 jar 中提取图像:

InputStream stream = this.getClass().getClassLoader()
                              .getResourceAsStream("/images/image.jpg");

您应该根据需要编辑图像的路径。

您的Image 代码将如下所示:

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

相关问题:

Java Swing Displaying Images From Within-a Jar

【讨论】:

  • +1 - 我记得当我第一次开始使用可执行 JAR 并从 Eclipse 中出来时遇到了这个问题。这不是很明显,所以一定要检查一下。
  • @酷,谢谢。我遇到这个问题的次数已经够多了,知道解决方案了。
  • 非常感谢您的回复。您的解释对我来说很有意义,但恐怕代码没有。我对Java有点陌生。我试过了,但得到“类型不匹配:无法从 BufferedImage 转换为 Image。”
  • @Thomas,嗯,我认为BufferedImage 是图像的子类。糟糕,至少我上面的答案有效。
  • @Justin:他使用的是com.itextpdf.text.Image 而不是java.awt.Image。它有助于检查进口。 ;)
【解决方案2】:

猜测,问题出在这一行:

Image image = Image.getInstance("penguin.jpg");

因为它在 src 目录中,所以它最终会出现在 JAR 文件中。但是,您不能直接从仅使用文件名的 JAR 文件加载文件。

但是,Image.getInstancean overload,它接受 URL,这使得这很容易:

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

/ 是 src 目录或 jar 文件的根目录,而不是文件系统根目录,如果您想知道的话。

【讨论】:

  • @Powerlord 我正在使用itext 7.我需要使用什么方法。我在API Image.getInstance 中找不到方法itext 7 API
【解决方案3】:

在不知道您遇到的错误的情况下,我猜这是一个 CLASSPATH 问题。当您从命令行运行 jar 文件时,您需要传递类路径以指向依赖的 jar 文件,或者系统类路径(环境变量)必须指向运行应用程序所需的所有 jar 文件。

【讨论】:

  • 不。在他的例子中,由于他使用 Eclipse 导出作为可运行的 jar,因此该信息包含在 .jar 清单中。
猜你喜欢
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多