我正在創建一個使用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
當您運行JAR時,是否看到任何錯誤消息? – 2010-09-01 14:15:45
不,我沒有收到任何錯誤消息。文件「Graphics and Text.pdf」只是沒有創建。 – Thomas 2010-09-01 14:29:07
即時通訊試圖瞭解是否有可能:你如何運行該jar?是可以在命令行上運行的jar,比如.exe? – djangofan 2011-02-03 01:03:01