【问题标题】:calling an exe file inside jar在jar中调用exe文件
【发布时间】:2009-09-06 12:41:30
【问题描述】:

我试图在这个 smartpdf 类所在的 jar 文件中调用“dspdf.exe”。我计划将其提取到临时位置并在程序结束时删除。但是这似乎不起作用,任何帮助将不胜感激。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.omg.CORBA.portable.InputStream;


public class smartpdf {
 static String url="";
 static String output="output.pdf";

public static void main(String[] args) throws IOException{
 gui mygui = new gui();//gui will call the generate function when user selects
}

 public static void generate() throws IOException{
  InputStream src = (InputStream) smartpdf.class.getResource("dspdf.exe").openStream();
  File exeTempFile = File.createTempFile("dspdf", ".exe");
  FileOutputStream out = new FileOutputStream(exeTempFile);
  byte[] temp = new byte[32768];
  int rc;
  while((rc = src.read(temp)) > 0)
      out.write(temp, 0, rc);
  src.close();
  out.close();
  exeTempFile.deleteOnExit();
  Runtime.getRuntime().exec(exeTempFile.toString()+" "+url+" "+output  );
  //Runtime.getRuntime().exec("dspdf "+url+" "+output);
 }

}

编辑: 我得到的错误:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassCastException: sun.net.www.protocol.jar.JarURLConnecti
on$JarURLInputStream cannot be cast to org.omg.CORBA.portable.InputStream
        at smartpdf.generate(smartpdf.java:18)
        at smartpdf.main(smartpdf.java:14)
        ... 5 more

【问题讨论】:

  • 帮助我们帮助您。 “似乎不起作用”可能意味着任何数量的事情。能否提供更多信息?
  • 抱歉,我已编辑问题以显示我遇到的错误。

标签: java jar executable-jar


【解决方案1】:

您使用了错误的 InputStream。将其更改为 java.io.InputStream。

【讨论】:

    【解决方案2】:

    你为什么用org.omg.CORBA.portable.InputStream而不是java.io.BufferedInputStream` 以来自资源的输入流作为参数。我的意思是:

    BufferedInputStream inputstream = new BufferedInputStream(smartpdf.class.getResourceAsStream(...));
    

    文件输出流也一样:BufferedOutputStream

    不要使用

    class.getResource(...).openStream();
    

    但使用

    class.getResourceAsStream(...);
    

    【讨论】:

      【解决方案3】:

      注意(一旦您解决了InputStream 问题)您应该使用您的衍生进程stdout 和stderr,否则衍生进程可能会阻塞。详情请见this answer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-13
        • 2010-10-10
        • 2018-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多