2012-10-04 24 views
0

我的包裝與我的罐子其他資源.exe文件解壓之後,和我用下面的代碼提取它:埃克給出錯誤從罐子

InputStream program=getClass().getResourceAsStream("/program.exe"); 
try { 
    FileOutputStream output=new FileOutputStream("C:\\Users\\Aitor\\Desktop\\program.exe"); 
    int b; 
    while ((b=program.read())!=1) 
    { 
     output.write(b); 
    } 
    output.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

,但我我試着執行生成的exe,我得到一個錯誤,說該壓縮文件的版本與我使用的windows版本不兼容。我怎麼能從jar提取一個exe文件而不會破壞它?

回答

1

我會使用的讀取更快的方式寫入文件:

FileOutputStream output=new FileOutputStream("C:\\Users\\Aitor\\Desktop\\program.exe"); 
int b = 0; 
byte[] buff = new byte[1024]; 
while ((b=program.read(buff))>=0) 
{ 
    output.write(buff, 0, b); 
} 
output.close(); 
+0

你是正確的,這樣的速度要快得多。但它並不能解釋爲什麼他的方式不起作用...... – AlexR

+0

你是否正在運行你的Java代碼中的exe文件?根據Windows版本,您需要提升該過程。 –

+0

謝謝!我會嘗試這個,併發布,如果它的工作。 @Gilberto,不,我只是提取它 – XaitormanX