1

I am trying to execute a bash script from Java with ProcessBuilder my code is :

 Process createUser = buildProcess(
 "/bin/su",
 "-c",
 "\"/opt/somedir/testdir/current/bin/psql",
 "--command",
 commandForUserCreation,
 /* "'select * from users'", */
 "--dbname",
 "mydbname\"",
 "myuser"
 );

The problem is that I receive error:

 /bin/su: unrecognized option '--dbname'

If I put echo in first place of my commands it prints correct command in bash and if I copy/paste this command it works!

Please, help me to resolve this issue.

asked Feb 3, 2014 at 11:06
0

2 Answers 2

2

You need to supply the whole command to execute by su as a single argument. Try this:

 Process createUser = buildProcess(
 "/bin/su",
 "-c",
 "/opt/vmware/vpostgres/current/bin/psql --command " + commandForUserCreation + " --dbname mydbname",
 myuser
 );
answered Feb 3, 2014 at 11:51
Sign up to request clarification or add additional context in comments.

Comments

0

This is what I use in processBuilder:

String[] command = new String[] {"echo", "Hello"};
String workspace = "/bin/su";
System.out.println("Trying to run command: "+ Arrays.toString(command));
ProcessBuilder probuilder = new ProcessBuilder(command);
probuilder.directory(new File(workspace));
Process process = probuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",Arrays.toString(command));
while ((line = br.readLine()) != null) {
 System.out.println(line);
}

I hope it helps.

answered Feb 3, 2014 at 11:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.