2009-09-06 135 views
3

我想調用這個smartpdf類存在的jar文件裏面的「dspdf.exe」。我計劃將其提取到臨時位置,並在程序結束時刪除。然而,這似乎並沒有工作,任何幫助將不勝感激。調用jar文件內的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 
+0

幫助我們來幫助你。 「似乎不起作用」可能意味着任何事情。你能提供更多的信息嗎? – 2009-09-06 12:45:50

+0

對不起,我編輯了這個問題來顯示我得到的錯誤。 – Hellnar 2009-09-06 12:52:34

回答

4

您使用錯誤的InputStream。將其更改爲java.io.InputStream。

1

爲什麼使用org.omg.CORBA.portable.InputStream而不是java.io.BufferedInputStream` ,其中as參數是資源的輸入流。我的意思是這樣的:

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

同樣爲您fileoutput流:的BufferedOutputStream

不要使用

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

但使用

class.getResourceAsStream(...); 
0

(一旦你已經解決了InputStream問題)如果你正在使用你的衍生過程stdout和stderr,否則產生的過程可能會被阻塞。有關更多詳細信息,請參閱this answer