1

Im trying to launch a jar file as a process, the command I need to run is something like this:

"java -Xmx512M -Xms512M -jar myapp.jar args"

And this is the code I currently have working:

ProcessBuilder builder = new ProcessBuilder();
String[] command = {"java", "-jar", "myapp.jar"};
builder.command(command);
Process process = builder.start();

OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

ReaderThread rt = new ReaderThread(reader);
Thread handle = new Thread(rt, "ReaderThread");
handle.start();

But if I try to add something like the "-Xmx1024M" I would get a "Maximun heap error" or if I try to add the "args" in the last string then I get "Unable to access jarfile".

So what is the right syntax to add all the paramaters for the system call to be done in the right order?

Thanks a lot for your help.

EDIT:

I also tried passing the whole command but for some reason that didnt work.

2 Answers 2

1

You don't appear to be handling the process's error input stream.

Please have a look here: What to do when Runtime exec Won't which will explain the problem and give you a solution: use stream gobblers.

Sign up to request clarification or add additional context in comments.

3 Comments

I do handle the streams later, but I just cant get it to execute the full command that I want. thx
I modified the original post to include that part, I can post the full code, but basically a thread reads the output, that part works I just cant pass the parameters to the sub process jar.
I use builder.redirectErrorStream(true), although is not in the post.
0

You may need to set the working (aka current) directory for the process so it can find the jar file. Or specify the classpath with -classpath so out includes that jar.

Also, do gobble the output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.