0

I am trying to create a GUI using java swing. From there I have to run linux system commands. I tried using exec(). But the exec() function is unable to parse the string if it contains single quotes. The code which I have used is as follows-

Process p = Runtime.getRuntime().exec("cpabe-enc pub_key message.txt '( it_department or ( marketing and manager ) )'")
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

But I am getting error when I run the program as--syntax error at "'(".

The same command runs when I write

Process p = Runtime.getRuntime().exec("cpabe-enc pub_key message.txt default")

Please help. Thanks in advance for your help.

3 Answers 3

1

Split up the parameters into an array instead, one string for each argument, and use the exec-method that takes as String[] instead, that generally works better for arguments. Somethign along the lines of:

Runtime.getRuntime().exec(new String[] {"cpabe-enc", "pub_key", "message.txt", "( it_department or ( marketing and manager ) )"});

or whatever what your exact parameters are.

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

Comments

0

Its because the runtime does not interpret the '(...)' as a single parameter like you intend.

Try using ProcessBuilder instead: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

Comments

0

I recently got this kind of problem solved. I was using javaFX to call shell scripts on button click .. which is very much similar to your swing application scenario...

Here are the links hope it might help you...

How to code in java to run unix shell script which use rSync internally in windows environment using cygwin?

Getting error in calling shell script in windows environment using java code and cygwin...!

Happy coding... :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.