0

I am developing a Java application where one of its task is to execute a system command using Runtime exec. The command requires another JAR file to be able to work but whenever the part where the command will be executed, I am getting an "Error: Unable to access jarfile".

Firstly, my current working directory:

assets/jarFile.jar
myApp.jar

Here's what my code looks like:

try {
  // To get the path of the directory where my current running JAR is
  String jarPath = myApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  String path = URLDecoder.decode(jarPath.substring(0, jarPath.lastIndexOf("/")), "UTF-8");

  // The system command to execute
  String command = "java -Xmx1024m -jar "+path+"/assets/jarFile.jar and-so-on...";

  Process p = Runtime.getRuntime().exec(command);
  ...
}

The jarFile.jar is not an executable JAR by the way.

Any idea?

Humble Potato II
  • 161
  • 1
  • 1
  • 12
  • Possible duplicate of [Error: Unable To Access Jar File](http://stackoverflow.com/questions/11943948/error-unable-to-access-jar-file) – NickL Jan 03 '17 at 18:08
  • @NickL I've also read that question and its answers before I've posted mine. We're in a different case. Not a possible duplicate. – Humble Potato II Jan 03 '17 at 18:11
  • Oke, so what have you tried? Different location? Wrong slashes in path? Strange symbols? Spaces? Have you tried enclosing it in quotes? Manually entering the full path in exec? Checked the permissions? – NickL Jan 03 '17 at 18:14
  • @NickL Yeah I tried a direct path `String command = "java -Xmx1024m -jar D:/assets/jarFile.jar and-so-on...";` still the same... – Humble Potato II Jan 03 '17 at 18:23
  • Same happens when you run that from terminal? – NickL Jan 03 '17 at 18:30
  • First get it to work from commandline. Have you tried different slashes? Backwards instead of forwards? Is the Jar file actually where you expect it to be? – NickL Jan 03 '17 at 18:41
  • Yes I test it first from the command line and it's absolutely working. I've also moved the `jarFile.jar` in a different folder in my desktop but still the same error occurs. – Humble Potato II Jan 03 '17 at 18:50
  • @NickL I've also renamed it and excluded the `.jar`extension as suggested [here](https://bukkit.org/threads/unable-to-access-jar-file.222494/#post-2194718) but still the same... – Humble Potato II Jan 03 '17 at 19:01
  • So you are saying that the EXACT same command that works on command line, with a full file path for the jar, does not work with runtime.exec? With that I really mean a main class with one line of code which is runtime.exec("java ....."), nothing else. And coincidentally you had the exact same error in another question 9 hours ago? Are you not leaving anything out here? – NickL Jan 03 '17 at 19:08
  • Yeah, dunno why it is happening. But nvm, now I got it working with full paths (good news). Seems like the problem is the the code to get the `jarPath` is returning a path which the system can't find. I'll work on it. I appreciate your time and help. Thank you! – Humble Potato II Jan 03 '17 at 19:24

1 Answers1

0

Seems like your code to find the path of the jar file does not return the correct path.

NickL
  • 4,258
  • 2
  • 21
  • 38