可以将 .exe 文件打包到 java jar 中,并将此 jar 添加到项目的部署程序集中,然后使用以下命令运行此 exe:
Runtime.getRuntime().exec();
我试图通过使用类加载器获取 glassfish 域内的路径后运行此 exe,但实际上此 exe 打包在 jar 内,这阻止了 Windows 执行它。
最佳答案
您必须首先从 jar 中提取它:
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()
在主机文件系统上运行它.
关于java - 使用应用服务器运行.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25690538/