构建jar后无法运行exe [英] Cannot run exe after building the jar
问题描述
我制作了一个简单的实用程序。在其中我有一个exe文件要运行。我通过使用来运行它:
I made a simple utility app. In it I had an exe file to be run. I got it to run by using:
Runtime.getRuntime().exec(this.getClass().getResource("filename.exe").getPath());
当我从ide(Netbeans)运行程序时,我的工作非常完美。
I works perfectly when I run the program from the ide(Netbeans).
但是当我尝试在构建之后使用上面的命令运行exe时(即从建筑物上创建的jar),它根本不起作用。
But when I try to run the exe using the above command after building(ie from the jar created on building) it does not work at all.
我也试过这个:
Desktop.getDesktop().open(new File("filename.exe"))
但不再使用。
请帮助
推荐答案
尝试用一个用例,发现getResource搜索路径file:/path/to/thejar.jar !filename.exe,并且无法在jar中使用exe。
Tried with a usecase, found that getResource search for path "file:/path/to/thejar.jar!filename.exe", and is unable to use the exe inside the jar.
尝试修改类路径,但失败了。
Tried modifying classpath, but failed.
参考spring的策略,它使用URL连接读取jar中的配置文件,
我认为解决方案可以是:
Refered to spring's strategy, which reads config files in jar with an URL connection, I think the solution can be :
-
使用getResourceAsStream获取一个InputStream
get an InputStream with getResourceAsStream
复制jar中的exe表单o使用ImputStream的临时文件
copy the exe form inside the jar to a temp file with the ImputStream
运行位于jar外部的临时exe文件。
run the temp exe file located outside the jar.
这样可行(编译为jar时可以,但IDE中的NOK,'导致'getResource在不同的地方搜索):
This works (OK when compiled to jar, but NOK in IDE, 'cause "getResource" search at different place):
package me.mren.loadresource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Runner {
/**
* @param args
*/
public static void main(String[] args) {
try {
String filename = "/resources/filename.exe";
System.out.println(Runner.class.getResource(filename));
InputStream fi = Runner.class.getResourceAsStream(filename);
File temp = File.createTempFile("temp_exe", "");
System.out.println(temp.getPath());
OutputStream fo = new FileOutputStream(temp);
byte[] b = new byte[1024];
int count = 0;
while ((count = fi.read(b)) != -1) {
fo.write(b, 0, count);
}
fi.close();
fo.close();
System.out.println(temp.canExecute());
Runtime.getRuntime().exec(temp.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
项目的文件结构:
C:\USERS\REN MIN\DEV ENV\JAVA\WORKSPACE\LOADRESOURCE
│ .classpath
│ .project
│ pom.xml
│
├─.settings
│ org.eclipse.jdt.core.prefs
│ org.eclipse.m2e.core.prefs
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─me
│ │ │ └─mren
│ │ │ └─loadresource
│ │ │ Runner.java
│ │ │
│ │ └─resources
│ │ filename.exe
│ │
│ └─test
│ ├─java
│ └─resources
└─target
│ loadresource-0.0.1-SNAPSHOT.jar
│
├─classes
│ │ filename.exe
│ │
│ └─me
│ └─mren
│ └─loadresource
│ Runner.class
│
├─maven-archiver
│ pom.properties
│
├─surefire
└─test-classes
jar中的文件结构:
the file structure inside the jar:
E:\TEST\RESULT
│ .classpath
│ .project
│ pom.xml
│
├─me
│ └─mren
│ └─loadresource
│ Runner.class
│
├─META-INF
│ MANIFEST.MF
│
└─resources
filename.exe
这篇关于构建jar后无法运行exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!