简体   繁体   中英

Why will shell script not execute from Java Runtime.exec but will from command line?

hope someone can help me here. Googled til my eyes bled and no luck, and tried everything I can think of.

I'm trying to execute a shell script which plays a song. When I type the following into the command line, everything's fine: /home/pi/startsong.sh /path/to/track

However, I'm trying to execute the script via a Java app, but get no response at all. Here's what I'm doing:

public void begin(String song) throws IOException, InterruptedException {

    //Split song title
    String[] cmd = song.split("");
    StringBuilder sb = new StringBuilder();

    //Ignore for now, this is just about sorting out titles with spaces, but irrelevant
    for (int i = 0; i < cmd.length; i++) {
        if (cmd[i].equals(" ")) {
            cmd[i] = " ";
        }
        sb.append(cmd[i]);
    }

    //Initiate bash script whilst passing song title as argument
    String command = "/home/pi/startsong.sh " + sb.toString();
    System.out.println(command);
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(command);
    p.waitFor();
    System.out.println("Playing song...");

}

When I run the program, the command it prints (as I ask it to) comes out exactly the same as what I would enter into the command line.

Why doesn't the script execute?

I have tried using ProcessBuilder and calling the program that plays the track directly but neither work. For simplicity I'm testing this with a track whose path has no spaces or strange characters.

I have tried adding /bin/bash -c to the beginning of the command string

FYI I'm running Java 8 on a Raspberry Pi running Raspbian. The program that plays the track is omxplayer.

Any help most gratefully received as I've been doing my nut in all day with this!

Thanks!

OK, thanks to @Etan I have managed to solve this. Modified the code thusly, using an InputStream reader to identify problems with filenames:

public void begin(String song) throws IOException, InterruptedException {

    //Split song title
    String[] cmd = song.split("");
    StringBuilder sb = new StringBuilder();

    //Ignore for now
    for (int i = 0; i < cmd.length; i++) {
        if (cmd[i].equals(" ")) {
            cmd[i] = " ";
        }
        sb.append(cmd[i]);
    }

    //Initiate bash script whilst passing song title as argument

    System.out.println("Playing song...");

    ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/home/pi/startsong.sh", "/home"+sb.toString());
    final Process process = pb.start();

    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;

    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    System.out.println("Program terminated!");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM