在jar文件中调用exe文件

3
我正在尝试调用位于jar文件中的"dspdf.exe",它与smartpdf类位于同一位置。我计划将其提取到临时位置,并在程序结束时删除。然而,这似乎无法实现,希望能得到帮助。
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

帮助我们来帮助你。“似乎不起作用”可能意味着很多事情。你能提供更多的信息吗? - Vinay Sajip
抱歉,我已经编辑了问题以展示我遇到的错误。 - Hellnar
3个回答

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

1
为什么要使用org.omg.CORBA.portable.InputStream而不是带有资源输入流参数的java.io.BufferedInputStream呢?我的意思是这个:
BufferedInputStream inputstream = new BufferedInputStream(smartpdf.class.getResourceAsStream(...));

对于您的文件输出流也是一样:BufferedOutputStream

不要使用

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

但使用

class.getResourceAsStream(...);

0
请注意,在解决了InputStream问题后,您还应该消耗生成的进程的标准输出和标准错误流,否则生成的进程可能会被阻塞。有关更多详细信息,请参见this answer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接