运行通过使用应用程序服务器的

问题描述:

它可以打包的.exe文件的Java罐内和这个jar添加到项目中展开组件比使用运行此exe .EXE:运行通过使用应用程序服务器的

Runtime.getRuntime().exec(); 我试图运行这个exe文件后,通过使用类加载器得到它在glassfish域中的路径,但实际上这个exe文件被封装在jar文件中,防止windows执行它。

你将不得不从罐子第一解压:

FileOutputStream fos=null; 
    InputStream is = null; 
    try 
    { 

     is = this.getClass().getResourceAsStream("path to your exe inside the jar"); 
     fos = new FileOutputStream("destination path on host file system"); 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = is.read(buffer)) > -1) 
      fos.write(buffer, 0, read); 

    } 
    finally 
    { 

     if (is != null) 
      is.close(); 
     if (fos != null) 
      fos.close(); 
    } 

然后,Runtime.getRuntime().exec()主机文件系统上运行它。

+0

提取是开销,因为它包含大量相关的dll和资源。我更喜欢将exe和相关的dll添加到项目中(不使用jar)而不是提取它。 – 2014-09-05 17:00:49