Java应用程序在Eclipse中正常运行,但不是.jar

问题描述:

我正在创建一个使用iText创建4个PDF文件的Java应用程序。在创建带有图像的PDF文件的情况下,.jar会创建一个0字节的文件并且不会继续执行。但是,当我右键单击>>运行方式>> Java应用程序时,它工作得很好。要创建JAR,我做了以下Java应用程序在Eclipse中正常运行,但不是.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); 
    } 
} 

}

感谢您的帮助!
Thomas

+2

当您运行JAR时,是否看到任何错误消息? – 2010-09-01 14:15:45

+0

不,我没有收到任何错误消息。文件“Graphics and Text.pdf”只是没有创建。 – Thomas 2010-09-01 14:29:07

+0

即时通讯试图了解是否有可能:你如何运行该jar?是可以在命令行上运行的jar,比如.exe? – djangofan 2011-02-03 01:03:01

在猜测,问题就出在这行:

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

由于这是在src目录下,它会在JAR文件结束。但是,您不能直接从JAR文件加载文件,只使用文件名。

然而,Image.getInstance具有an overload,需要一个URL,这使得这个相当容易:

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

/是src目录或jar文件(S),而不是文件系统根目录的根目录,如果你我想知道。

+0

谢谢!这工作。 – Thomas 2010-09-01 14:44:20

+0

@Powerlord我正在使用itext 7.我需要使用哪种方法。我在API [itext 7 API](http://itextsupport.com/apidocs/itext7)中找不到方法Image.getInstance /7.0.1/) – Pradeep 2016-12-29 13:21:09

托马斯,问题是当你创建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

+1 - 我记得当我第一次开始使用可执行JAR并从Eclipse出来时遇到了这个问题。这并不明显,所以一定要检查一下。 – JasCav 2010-09-01 14:19:45

+0

啊哈......观察贾斯汀! – CoolBeans 2010-09-01 14:19:59

+0

@冷静,谢谢。我有足够多的时间来解决这个问题。 – jjnguy 2010-09-01 14:21:22

不知道你所得到的错误,我想这是一个CLASSPATH问题。当从命令行运行jar文件时,您需要传递classpath以指向相关的jar文件,或者系统classpath(环境变量)必须指向运行应用程序所需的所有jar文件。

+0

nope。在他的情况下,由于他使用Eclipse导出作为可运行jar,该信息包含在.jar清单中。 – djangofan 2011-02-03 01:03:51