-3

I have gpg command which encrypts the file

 gpg --batch --yes -o abc.csv.gpg -r 'balu shanmukh' -e A2.java

in the above command

--batch
--yes
-o 
-r and -e are options

and balu shanmukh is the value.

the command is prepared dynamically and the command prepaed is the above. this command is being exected in commandline but when this command is running from

 Runtime.getRuntime().exec(command);

am getting error message as usage: gpg [options] [filename]

their is no problem with gpg classs path or anything because if i remove the single quote for the value which doesnt have space in between the command is getting executed properly from java.

only the problem when we have space in value . then its mandatory to use quote for value and if i use it . commad is not getting executed properly

I didnt understand the behaviour? can any one help me to understand this behaviour

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
phanikiran
  • 75
  • 1
  • 6
  • and waht about your code? the piece where you create the command to be executed (the gpg string) ? – woliveirajr Jul 08 '11 at 12:39
  • possible duplicate of [How to execute command with parameters?](http://stackoverflow.com/questions/7134486/how-to-execute-command-with-parameters) – Raedwald Jan 07 '15 at 20:49

2 Answers2

1

Don't use Runtime().exec(), and don't create the command as a single string. Instead, build a ProcessBuilder for your Process, and pass each switch to your command as an argument.

Here's a simple example:

Process process = new ProcessBuilder("gpg", "--foo", "-r", "some_file_name").start();
int exitStatus  = process.waitFor();

PS: As others noted, do revisit your questions, and check the answers you think answer your question. People give some effort answering. If you think they helped, vote them up, or check their answer as the one that answered your question.
Good luck and welcome here ;)

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
0

Here's a simple example:

Process process = new ProcessBuilder("gpg", "--foo", "-r", "some_file_name").start(); int exitStatus = process.waitFor();

Sorry but that doesnt explain how you pass a single quoted string as an argument. ie. to pass 'baku' as an argument. "'baku'" does not work....

user1048271
  • 141
  • 1
  • 4