gilles bertrand
gilles bertrand

Reputation: 21

Execute bash in java

Wen i try to run this code :

String[] cmd = {"/bin/bash", "-c", "printf '%s'\n"+videoPath+"./"+"*.mp4 >"+"mylist.txt"}; 

processBuilder.command(cmd);

I get some error:

/bin/bash: line 1: /home/gilles/eclipse-workspace/informationGewinnungApp/videotool/outputs/./info.mp4: cannot execute binary file: Exec format error 126

Upvotes: 0

Views: 81

Answers (1)

user1934428
user1934428

Reputation: 22311

The \n in your string is expanded into a newline. Hence bash sees two commands,

printf %s
..../info.mp4

Do it either as

String[] cmd = {"/bin/bash", "-c", "printf '%s' "+videoPath+"./"+"*.mp4 >"+"mylist.txt"}; 

Or

String[] cmd = {"/bin/bash", "-c", "echo "+videoPath+"./"+"*.mp4 >"+"mylist.txt"}; 

But: Why don't you want to use a bash child process, if you only want to create a new file containing a certain string? Wouldn't it be easier to do it directly from Java?

Upvotes: 2

Related Questions