简体   繁体   中英

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

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)

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.

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?

PD: Using this code, it return me 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. 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. 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)

  1. A better solution :

Use java to copy the file to the working directory and execute it from there.

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 :)

This way it will keep the version of the jar locally and won't have to download it every time. 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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