0

I'm trying to execute the following command using java processbuilder

ls | xargs cat > pathtonewFile

Currently my code is

 String command="ls | xargs cat >t"

 ProcessBuilder probuilder = new ProcessBuilder(command);
 probuilder.start()

Currently this executes without an error but a new file is not created.

I've tried below based on another post here: Problem Using Java ProcessBuilder to Execute a Piped Command

   ProcessBuilder probuilder = new ProcessBuilder("sh", "-c", "ls  | xargs cat >        pathtonewfile");

The command executes but creates a 0 byte file.

Community
  • 1
  • 1
user1127081
  • 161
  • 1
  • 3
  • 13
  • It strikes me that this problem is most likely to be in the command that you are executing. Try running the same command from a shell prompt. One possibility is that the command is being run with the wrong current directory. – Stephen C Aug 16 '12 at 02:44

1 Answers1

0

Try adding the file extension to the path, or adding the file extension to the name of the file.

Bl H
  • 133
  • 4
  • 12
  • Stephen C - if I execute from the command line it works as expected – user1127081 Aug 16 '12 at 13:18
  • I figured out the problem the command ProcessBuilder probuilder = new ProcessBuilder("sh", "-c", "ls | xargs cat > pathtonewfile"); works the problem was the folder where ls was executed contained a folder which was causing an error. The fix was to add a filter to ls (ls *.txt") – user1127081 Aug 16 '12 at 13:53