简体   繁体   中英

Runtime.getRuntime().exec() does not execute external client jar

So basically I own an online game that players can connect to by using a client application written in Java. I wrote a launcher application (also in Java) that has an automatic update system, so our users don't have to manually re-download the client after each update.

Now lately I have been getting reports from some users whose launcher opened and ran fine. However, at the point where it executes the command for starting the client application, nothing happens. Now I am guessing this has to do with some kind of security settings in certain desktop environments, the problem doesn't seem platform or JDK specific. Both the launcher and the client use JDK8. However, I don't see how this could matter as I read somewhere that Oracle forced all users to update their JDK to 8.

Here is a link to the full class:

https://pastebin.com/fF21d4vu

Here is the specific code that runs the external client jar:

private static void startApplication() {
        try {
            Runtime.getRuntime().exec("java -jar "+(findCacheDir() + "client.jar")+" "+ARGS[0]);
            Thread.sleep(1000);
            System.exit(0);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

Does anyone have an idea what might go wrong or what approach I should take to address this? I would love to know about a universal, platform-independent way of ensuring the code will execute.

Thanks in advance,

Stan

You are doing a couple of things wrong.

  1. You are throwing away the Process returned by exec
  2. You are not checking its return code
  3. You are not waiting for it to complete. (The sleep(1000) hack does not cut it!)
  4. You are not checking the standard output / error streams that it might be reporting errors to.

Read the javadocs for Process to understand how to do the above.

The actual problem could be one or more of the following:

  • You are relying on exec(String) to split the command line into arguments. If any of the arguments contains white space, that will fail. (And quoting or escaping won't help!!) Use the exec(String[]) overload and split the arguments yourself.

  • It could be a problem with the %PATH% / $PATH environment variable meaning that the OS cannot find the java command.

  • It could be that the path to the JAR file is incorrect
  • It could be that you have the arguments incorrect
  • The JAR may not be a properly formed executable JAR file
  • The program in the JAR could just be buggy

Note that if you do the things at the beginning correctly there should be evidence to help you figure our what the actual problem is.


I would love to know about a universal, platform-independent way of ensuring the code will execute.

Wouldn't we all. Unfortunately, there isn't one. Even if you correct all of the possible problems / mistakes in the above, there are still circumstances under which it won't work. For example, if either the Java sandbox or the OS doesn't permit you to run java . (Think ... AppArmor, or SE Linux in "enforcing" mode.)

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