Jesse Naugher
Jesse Naugher

Reputation: 9820

Java Runtime.exec() with ssh on Linux Issues

I am trying to start a new process using Runtime.exec(), but my problem lies within using ssh to remote in and then run a java program there. Code:

test = "ssh -t username@host java packageName.ClassName portNumber (Other command line args for this class)"
Process proc = Runtime.getRuntime().exec(new String[] {"/bin/bash", "-c", test});

this doesn't fail or catch, but I need to be able to see the stdout for the newly running process and I don't.

Note: if I run ssh -t username@host java packageName.ClassName portNumber (Other command line args for this class) from the command line it works fine. I have the host setup to not require a password by using ssh keys.

Any ideas?

Upvotes: 0

Views: 4324

Answers (3)

Tim Bender
Tim Bender

Reputation: 20442

You need to use Process.getInputStream to obtain the output from the sub-process being created.

See this article for a good discussion on Runtime.exec.

Upvotes: 2

Adam Zalcman
Adam Zalcman

Reputation: 27233

Use getInputStream() to access returned process's stdout. You can also use facilities provided by ProcessBuilder.Redirect.

Upvotes: 1

I think you can ask for an input stream that corresponds to the stdout of the process and then print it on your standard output. If you need to see it after it executes, just call waitFor() method on the process so it finishes before you start printing.

Upvotes: 1

Related Questions