1

When I execute this line of code:

    Runtime.getRuntime().exec(new String[] {"mkdir", "-p", "/home/stuff/Keyring", "&& touch", "/home/stuff/Keyring/keyring.gpg"});

the messaging.gpg inside the Keyring folder is created as a directory instead of a file and I can't figure out why. Any ideas?

Samuel
  • 21
  • 4

1 Answers1

6

You're executing mkdir with the following arguments:

"-p", "/home/stuff/Keyring", "&& touch", "/home/stuff/Keyring/keyring.gpg"

(the first argument to exec() is the process to execute) so the -p tells mkdir to build parent directories if required, and the remaining arguments are the directories to create. Hence your problem/issue (I suspect you'll have a dir somewhere called '&& touch')

It looks like you want to execute a shell script, so you need to encapsulate the above as such e.g. provide arguments such as:

/bin/sh -c "mkdir -p /home/stuff/Keyring && touch /home/stuff/Keyring/keyring.gpg"

i.e. you're executing /bin/sh, and providing the commands on the shell command line.

Or better still, use the java.io.File API or similar, and avoid forking processes altogether?

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440