Unable to run gpg command in JAVA

Steve Revilak steve at srevilak.net
Fri Aug 15 03:42:01 CEST 2008


> From: nishant sonone
> Subject: Unable to run gpg command in JAVA

> I am porting an existing perl-mason code to java.
> I need to create certificates for certain inputs.
> The command used on perl-mason was
>
> open(COMMAND, "echo $text | gpg --homedir $ENV{HOME}/.gnupg -s -u \"The
> Certificate Key\" |");
> my $enc = join('',<COMMAND>);
> my $encCert = encode_base64($encrypted);
>
> When i tried to use the dame command in java, its not able to recognize the
> input to '-u' option.
> I think java is not able to interpret the spaces between the words of input
> string  \"The Certificate Key\".

The perl seems reasonable, but what does your java code look like? :)

If you gave the entire command line as a single string, then you're at
the mercy of java's tokenization of the command line.  For example,
the javadoc for java.lang.Runtime says this:

   More precisely, the command string is broken into tokens using a
   StringTokenizer created by the call new StringTokenizer(command) with
   no further modification of the character categories. The tokens
   produced by the tokenizer are then placed in the new string array
   cmdarray, in the same order.

My guess is that `"The', `Certificate', and `Key"', are being treated
as three separate arguments.

Try providing the command as a String[], e.g.

   String cmd[] = {
      "gpg",
     "--homedir",
     System.getProperty("user.dir") + File.separator + ".gnupg",
     "-s",
     "-u",
     "The Certificate Key"
   };

   runtime.exec(cmd);



Steve



More information about the Gnupg-users mailing list