简体   繁体   English

为何运行该文件时,除非我在Jar所在的文件夹中,否则为什么我无法访问其文件?

[英]Why can't I access a file within my jar unless I'm in the folder with the Jar when I run it?

I recently created an application and successfully jarred this to c:/my/folder/app.jar. 我最近创建了一个应用程序,并将其成功地添加到c:/my/folder/app.jar。 It works like a charm in the following case [Startup #1]: 在以下情况下[启动#1],它的工作原理像一种魅力:

  • Open cmd 打开cmd
  • cd to c:/my/folder cd到c:/ my / folder
  • java -jar app.jar java -jar app.jar

But when I do this, it doesn't work [Startup #2]: 但是当我这样做时,它不起作用[启动#2]:

  • Open cmd 打开cmd
  • cd to c:/my/ cd到c:/ my /
  • java -jar folder/app.jar java -jar文件夹/app.jar

Because app.jar contains a .exe-file which I try to run in my application: 因为app.jar包含一个.exe文件,我试图在我的应用程序中运行它:

final Process p = Runtime.getRuntime().exec("rybka.exe");

It won't work in example 2 because it can't find the file rybka.exe. 由于找不到文件rybka.exe,因此无法在示例2中运行。

Any suggestions? 有什么建议么?

Something like this is a better way forward. 这样的事情是更好的前进方式。 Copy the exe out of the jar to a temp location and run it from there. 将exe从jar中复制到临时位置,然后从那里运行它。 Your jar will then also be executable via webstart and so on: 然后,您的jar也可以通过webstart等执行:

InputStream src = MyClass.class.getResource("rybka.exe").openStream();
File exeTempFile = File.createTempFile("rybka", ".exe");
FileOutputStream out = new FileOutputStream(exeTempFile);
byte[] temp = new byte[32768];
int rc;
while((rc = src.read(temp)) > 0)
    out.write(temp, 0, rc);
src.close();
out.close();
exeTempFile.deleteOnExit();
Runtime.getRuntime().exec(exeTempFile.toString());

If the jar will always be in that directory you can use a full path /my/folder/rybka.exe . 如果jar始终位于该目录中,则可以使用完整路径/my/folder/rybka.exe If not, you can use getClass().getProtectionDomain().getCodeSource().getLocation() to find out the location of the jar and prepend that onto rybka.exe . 如果不是,则可以使用getClass().getProtectionDomain().getCodeSource().getLocation()来找出jar的位置,并将其放在rybka.exe

Try extracting the exe to 尝试将exe解压缩到

System.getProperty("java.io.tmpdir"));

then run it from this location too should work every time. 然后从该位置运行它也应该每次都能工作。

Paul 保罗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么除非解压缩文件,我的Jar才会运行? - Why my Jar doesn't run unless I extract files? 为什么我不能正常运行我的 .jar 文件? (苹果系统) - Why can't I run my .jar file normally? (MacOS) 我无法在Windows Vista上运行我的JAR文件 - I can't run my JAR file on Windows Vista 我无法运行 JAR 文件? - I can't run JAR file? 当我从jar运行我的应用程序时,为什么新的File(path)不起作用? - Why does new File(path) doesn't work when I run my application from jar? 当类在我的JAR文件中时,为什么会得到一个无法加载的类? - Why am I getting a can't load class, when the class is in my JAR file? 当它需要jar中的类时,为什么不能在cygwin上编译我的Java文件? - Why can't I compile my java file on cygwin, when it needs classes from a jar? 如何从可执行的Jar文件中加载xml资源文件并将其保存到jar所在的文件夹中? - How can I load an xml resource file from within an executible Jar file and save it to the folder the jar is located in? 运行生成的 jar 文件时,如何使 Apache Derby 数据库正常工作? - How can I make my Apache Derby database work when I run my generated jar file? 运行proguard时,是否可以只用一句话就将jar文件中的所有内容都保留? - Can I keep all the contents in my jar file in just one sentence when I run proguard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM