java runnable jar_java – 找不到Runnable JAR文件异常

您需要执行getResourceAsStream,并确保将pdf内置到jar中.因为你在源文件夹中有它们,所以它们应该.

假设您在src / resources / orderForm.pdf下有pdf,它将最终作为/resources/orderForm.pdf放在jar文件中.您将为/resources/orderForm.pdf打开资源流.

如果你必须有一个诚实的善良文件,那么你需要将PDF作为资源流读取的代码,FileOutput将其输出到临时文件,然后使用该文件.或者,你根本无法将jar包装在jar中.

public class PDFWriter {

public static final String testDir = "C:\\pdftest\\";

public static final String adobePath = "\"C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe\"";

public static void main(String[] args){

try {

new PDFWriter().run();

} catch (Exception e) {

e.printStackTrace();

}

}

public void run() throws Exception {

InputStream in = this.getClass().getResourceAsStream("/resources/test.pdf");

new File(testDir).mkdirs();

String pdfFilePath = testDir + "test.pdf";

FileOutputStream out = new FileOutputStream (pdfFilePath);

byte[] buffer = new byte[1024];

int len = in.read(buffer);

while (len != -1) {

out.write(buffer,len);

len = in.read(buffer);

}

out.close();

Runtime rt = Runtime.getRuntime();

String command = adobePath + " " + pdfFilePath;

rt.exec(command);

}

}

文件方式在eclipse中工作的唯一原因是因为bin文件夹是一个真正的文件夹,但是当你构建这个东西时,它们都会被压缩到一个jar中.

也许对源文件夹有点困惑.如果你有一个名为’src’的源文件夹,它下面的包结构不包含“src”. src / net / whatever / Class.java在构建时将变为net / whatever / Class.class.

因此,您可以创建一个名为’rsrc'(资源)的第二个源文件夹,并在此下放置您的resources / order.pdf.构建jar时,rsrc / resources / order.pdf将成为resources.order.pdf.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值