我做了一个简单的实用程序。 其中我有一个exe文件要运行。 我用它来运行它:
Runtime.getRuntime().exec(this.getClass().getResource("filename.exe").getPath());
当我从ide(Netbeans)运行程序时,我工作得很好。
但是当我尝试使用上面的命令运行exe文件后(即从build立的jar),它根本不工作。
我也试过运行这个:
Nginx + PHP5-fpm不显示PHP错误,但cli显示错误
尝试在Windows上编译时出错
在Windows下运行Objective-C应用程序失败
x86 GNU汇编奇怪的更改Seg错误
Windows 7(64)上的C ++ CreateProcess错误代码2(“ERROR_FILE_NOT_FOUND”)
Desktop.getDesktop().open(new File("filename.exe"))
但没有再次使用。
请帮忙
如何在MsgBox中使用VBS中的计算机名称环境variables?
Matplotlib-Animation“No MovieWriters Available”
在Linux下使用Atlas在numpy中的线程错误
winutils.exe已停止工作
尝试用一个用例,发现getResource搜索路径“file:/path/to/thejar.jar!filename.exe”,并无法使用该jar内的exe文件。
尝试修改类路径,但失败了。
提到spring的策略,它读取jar中的URL配置文件,我认为解决方案可以是:
使用getResourceAsstream获取InputStream
使用ImputStream将jar内的exe表单复制到临时文件中
运行位于jar外的temp exe文件。
这工作(确定当编译到jar,但在IDE中的NOK,因为“getResource”在不同的地方搜索):
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,count); } fi.close(); fo.close(); System.out.println(temp.canExecute()); Runtime.getRuntime().exec(temp.getPath()); } catch (Exception e) { e.printstacktrace(); } } }
项目的文件结构:
C:USERSREN MINDEV ENVJAVAWORKSPACELOADRESOURCE │ .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里面的文件结构:
E:TESTRESULT │ .classpath │ .project │ pom.xml │ ├─me │ └─mren │ └─loadresource │ Runner.class │ ├─meta-inf │ MANIFEST.MF │ └─resources filename.exe
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。