0

i'm trying to write a program ,that have toexecute the same code in linux terminal:

openssl req -passout pass:abc -subj /C=US/ST=IL/L=Chicago/O=IBM          Corporation/OU=IBM Software Group/CN=John Smith/[email protected] -new > johnsmith.cert.csr

In the terminal it works fine, but in Java it didn't. I try something like this, but without result.

String[] cmd = { "openssl", "req -passout pass:abc -subj", "/C=US/ST=IL/L=Chicago/O=IBM          Corporation/OU=IBM Software Group/CN=John Smith/[email protected]", "-new > johnsmith.cert.csr" };
Runtime.getRuntime().exec(cmd);

Can you explain me, what i miss.Thanks in advance. Best wishes Andrey

3
  • 2
    Have you tried: Runtime.getRuntime().exec("openssl req -passout pass:abc -subj /C=US/ST=IL/L=Chicago/O=IBM Corporation/OU=IBM Software Group/CN=John Smith/[email protected] -new > johnsmith.cert.csr");? Commented Jun 19, 2013 at 15:59
  • 2
    Did you see any error? If yes, what was the error message? Commented Jun 19, 2013 at 15:59
  • Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces. Commented Jun 19, 2013 at 17:14

1 Answer 1

1

You are missing the fact that stream redirection > is a functionality of shell that does not exist here.

You can either prepend your command with /bin/sh -c or redirect output using java:

Process proc = Runtime.getRuntime().exec(cmd);
InputStream in = proc.setOutputStream();
OutputStream out = new FileOutputStream("johnsmith.cert.csr");
int b;
while( (b = in.read()) != -1) {
   out.write(b);
}
out.flush();
out.close();

Now you can remove "> johnsmith.cert.csr" from your command line. I personally like this solution more.

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

1 Comment

Thank you.I found that i have syntacsys error in command, but you are right for the file.Thank you

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.