简体   繁体   English

使用Java使用Runtime.getRuntime()。exec打开外部.jar

[英]Open external .jar with Runtime.getRuntime().exec with java

So I have this code that works fine, it launch the .jar file from another machine that I have configure in my pc as a red ubication 所以我有这段代码可以正常工作,它从我在PC中配置为红色ubication的另一台计算机启动.jar文件

Runtime.getRuntime().exec("java -jar Z:\\AAA\\BBB\\CCC\\ZZZ.jar");

But now I want to launch the .jar from that external path without using the shortcut before (so I can launch it with this code in a machine that dont have that red ubication configured) 但是现在我想从外部路径启动.jar而不使用快捷方式(所以我可以在未配置红色泛滥的机器上使用此代码启动它)

Runtime.getRuntime().exec("java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");

But doent work (I can access and open the file manually without a problem). 但是可以完成工作(我可以手动访问和打开文件而不会出现问题)。

When I enter the java -jar MMM\\\\NNN LLL\\\\OOO\\\\AAA\\\\BBB\\\\CCC\\\\ZZZ.jar in the Command prompt it return me Error: Unable to access jarfile MMM\\NNN , so perhaps one problem is that the path have a space in the folder name, but I think that may be something else too. 当我在命令提示符下输入java -jar MMM\\\\NNN LLL\\\\OOO\\\\AAA\\\\BBB\\\\CCC\\\\ZZZ.jar ,它返回我Error: Unable to access jarfile MMM\\NNN ,所以可能是一个问题是路径在文件夹名称中有一个空格,但是我认为这可能也是其他原因。

The question is, if the problem is the space, how I can solve it? 问题是,如果问题是空间,我该如何解决? I cant find a way. 我找不到办法。 And in the other hand, how I can run it in another machine? 另一方面,我如何在另一台机器上运行它? I have to use that red ubication IP in some way instead? 我必须以某种方式使用那个红色的泛化IP?

PD: Using this code, it return me true PD:使用此代码,它返回我为true

File f = new File("\\\\MMM\\NNN LLL\\OOO\\ZZZ.jar");
System.out.println(f.exists()); //--> true

So looks like the spaces dont interfere in the path (the four "\\" doesnt seem to do anything in the tests when launching) 因此,看起来空格不会干扰路径(启动时测试中的四个“ \\”似乎没有任何作用)

I have heard other people having such problems. 我听说其他人有这样的问题。 The main reason for that is that probably Java exec method is not network (SMB) aware. 这样做的主要原因是,Java exec方法可能不支持网络(SMB)。 So it doesn't even try to open from the network. 因此,它甚至都不会尝试从网络打开。

Anyway running the code like that from the network might not be the best solution. 无论如何,从网络运行这样的代码可能不是最佳解决方案。 First of all the network might be unavailable, or the java file coming might be corrupted. 首先,网络可能不可用,或者即将到来的Java文件可能已损坏。 If you want to do it properly you have several options: 如果您想正确执行此操作,则有以下几种选择:

  1. Simple option that can work: 可以工作的简单选项:

Create a bat file that works and exec that one - you can even copy the file locally first to make sure it is available first (if it is big and the network fails) 创建一个有效的bat文件并执行该文件-您甚至可以先在本地复制该文件,以确保该文件首先可用(如果文件很大并且网络出现故障)

  1. A better solution : 更好的解决方案:

Use java to copy the file to the working directory and execute it from there. 使用java将文件复制到工作目录并从那里执行。

A plus to do it like that by downloading is that you can maintain a version of the file (hash?) so you don't download it if it is the same. 通过下载执行此操作的一个好处是可以维护文件的版本(哈希?),因此如果文件相同,则不必下载。 Or you can have fallback - execute the last downloaded version if the network drive is unavailable. 或者,您也可以回退-如果网络驱动器不可用,请执行上次下载的版本。

  1. Use a local maven repository and dependency for that jar :) 为该jar使用本地Maven存储库和依赖项:)

This way it will keep the version of the jar locally and won't have to download it every time. 这样,它将把jar的版本保留在本地,而不必每次都下载。 It will also download a new version if available and the code will be more mainstream (for example not platform / pc dependent) 如果可用,它还将下载一个新版本,并且代码将成为主流(例如,不依赖于平台/个人计算机)

The answer give by @MadProgrammer works fine! @MadProgrammer给出的答案很好用!

ProcessBuilder builder = new ProcessBuilder("java",  "-jar", "MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
try {
    builder.start();
} catch (IOException e) {
    e.printStackTrace();
}   

Lot of thanks! 非常感谢! In any case going to check the ideas posted by Veselin Davidov 无论如何要检查Veselin Davidov发表的想法

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM