4

I need to start jar and provide input to it. I've found how to start jar, which works without problems, using Runtime#getRuntime#exec, and I've also found that

String command = "stop";
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bufferedWriter.write(String.format("%s\n", command));
bufferedWriter.flush();

should do the job. But the problem is, it just doesn't work for me, it literally does nothing. Does anyone know how to do that? Process:

    public static Process startJar(File jarFile, String flags, String args, @Nullable File dir) throws IOException {
        if (dir == null){

            return Runtime.getRuntime().exec(String.format("cmd /c start /wait \"\" java -jar %s \"%s\" %s",flags ,jarFile.getAbsolutePath(), args));

        }
        return Runtime.getRuntime().exec(String.format("cmd /c start /wait \"\" java -jar %s \"%s\" %s", flags, jarFile.getAbsolutePath(), args), null, dir);


    }
2
  • Can we see more code? What's process? Commented Jun 17, 2020 at 16:08
  • I think your question/problem is similar to this one. The answer I made relies on the ProcessBuilder class, you may want to take a look at. Commented Jun 17, 2020 at 16:31

3 Answers 3

2

If the input is commandline args, pass it to exec directly.

If not, and assuming process is the other jar you are running, you'll need to write to the InputStream of process, not the OutputStream. process.getOutputStream() will give you the stream to which the process is outputting its results, not where it's reading its input from.

EDIT: After Taschi pointed out that the code is correct

I found another question similar to yours. The accepted answer states that you have to either close the writer or pass a \n in order for it to work. Try that. If it still doesn't work, make sure the other JAR you're running is actually waiting for input on its STDIN.

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

15 Comments

That was my first suspicion but according to the API documentation, it is wrong: getOutputStream() is the stream through which you can send output TO the other process, and getInputStream() is the stream through which you get input from it.
Thanks, I will try it.
@Taschi Can you share the exact link of the page where you read this, please?
Sorry, I don't know how to write into InputStream, and I doubt it is even possible, can you please tell me how?
|
1

I assume your process is blocked because it tried to write something to its output, and you did not read it. The API doc states:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

So, read from process.getInputStream() and process.getErrorStream() unless both of them are empty, and then it should process your input just fine.

See here: Starting a process in Java? for a possible example on how to read.

5 Comments

Should InputStream#reset do the job?
Possibly? But I doubt it.
reading through both streams didn't solve anything.
Have you tried dumping the other program's output to the console? That might give you some insight as to what is going on. Also, does the program react to input if you run it on the command line, rather than launch it from inside your program?
Rather than explicitly reading from the input streams, you could redirect them to the std output and error streams of your master program. Of course that means that outputs of the master and child program will be mixed together but it may be good enough to start debugging your programs.
0

The problem was, that I was starting that jar in a new window (cmd /c start), after removing that, everything works without a problem.

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.