IZED
IZED

Reputation: 11

Java: Runtime.exec() only work thru console

I got a problem, I made a Java application, that downloads another Java Application (Jar) and saves it in appdata, then it should run it and this works too. But my Runtime.exec() I use to run the second Jar only works when I run the main Jar thru console.

String command = "java -jar -Xms" + comboBox.getSelectedItem() + " " 
    + Util.getWorkingDirectory() + File.separator + "zlauncher" 
    + File.separator + "minecraftStarter.jar " + txtUsername.getText() + " " 
    + passwordField.getText() + " " + Util.toString(chckbxRemember.isSelected());
Runtime rt = Runtime.getRuntime();
rt.exec(command);

When starting this thru console, it works, but not when I run it as stand-alone Jar.

Can you give me a tip, how I can visualize a way of starting the second Jar without the need of a console ?

EDIT: I tried it also with ProcessBuilder, same result. (Works in console, but not as stand-alone)

Upvotes: 1

Views: 686

Answers (2)

Jaec
Jaec

Reputation: 390

You shuld put the command into a String array:

Runtime rt = Runtime.getRuntime();
rt.exec(new String[] { "java", "-jar", "-Xms", comboBox.getSelectedItem(), Util.getWorkingDirectory() + File.separator + "zlauncher" + File.separator + ""minecraftStarter.jar", txtUsername.getText(), passwordField.getText(), Util.toString(chckbxRemember.isSelected()) });

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

A few tips:

Just because you couldn't get it to work differently using ProcessBuilder doesn't mean that you should give up and use Runtime.exec(). Runtime.exec() is somewhere between really difficult and impossible to use for anything non-trival. So use ProcessBuilder - you'll be happy you did.

Use the ProcessBuilder constructor that takes a list, not the one that takes a String. The one that takes a string has exactly the same problems as Runtime.exec()

If possible, use a batch file for Windows, or a shell script for Linux, that does as much of the work for you as possible (setting paths, etc.). Once you've debugged that at the command-line, you'll have a much easier time using it with ProcessBuilder.

Invoke your environment's shell (e.g. cmd /e or /bin/bash, or what-have-you, and have the shell execute the batch file.

Don't forget to read the output streams from the spawned process. If you don't, the child process will very likely lock up.

Upvotes: 3

Related Questions