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.

1
  • 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. Commented Aug 16, 2012 at 2:44

1 Answer 1

0

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

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

2 Comments

Stephen C - if I execute from the command line it works as expected
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")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.