0

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)

Grant Foster
  • 722
  • 2
  • 11
  • 21
  • If the problem is a space you can solve it by adding "" in the command prompet. You can try java -jar "MMM\NNN LLL\OOO\AAA\BBB\CCC"\ZZZ.jar from command prompt – Veselin Davidov Jan 23 '18 at 09:22
  • On which OS you are ? Is it windows? How long the real path is? Windows has a limitation on the length of a path – navy1978 Jan 23 '18 at 09:24
  • Start by considering using `ProcessBuilder` over `Runtime.exec` as it provides better configurability, as an [example](https://stackoverflow.com/questions/14999489/processbuilder-cannot-find-the-specified-file-while-process-can/14999611#14999611) – MadProgrammer Jan 23 '18 at 09:26
  • ->Veselin Davidov I suppose you mean put something like ...NNN"+" "+"LLL... ? Or to treat just the part of the path as an separate String? ->navy1978 is Windows7 pro, the length is around 100 chars ->MadProgrammer going to give a shot to that – Frikilangelo Jan 23 '18 at 10:39

2 Answers2

0

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)

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • I will try that second option, perhaps "force" to download the .jar at temp and deleting from there at exit its a viable option. The 3rd one dont know how to do it, so for now I will check the other solutions and see what happens – Frikilangelo Jan 23 '18 at 10:44
0

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