Search code examples
javalinuxprocessbuilder

Java ProcessBuilder: command not found


I am trying to run some Java code I found on linux.

    maudecmd = new String[files.length+5];
    maudecmd[0] = "maude";
    maudecmd[1] = "-no-banner";
    maudecmd[2] = "-no-ansi-color";
    maudecmd[3] = "-no-mixfix";
    maudecmd[4] = "-no-wrap";
    for(int i = 0; i < files.length; ++i) {
        maudecmd[5+i] = files[i];
    }

    ProcessBuilder pb = new ProcessBuilder(maudecmd);
    if(dir != null)
        pb.directory(dir);
    pb.redirectErrorStream(true);
    maude = pb.start();

This throws an IOException - bash can't find the 'maude' command.

I have this aliased in my .bashrc file though:

alias maude='~/lib/maude/maude.linux'

If I change the code like this:

maudecmd[0] = "/u/h/os215/lib/maude/maude.linux";

It works fine.

I want to change this code so it is more robust - if someone can run Maude on the command line the ProcessBuilder should also be able to use it, whatever specific method the user has to link Maude up.

Can this be acheived?


Solution

  • .bashrc aliases only affects the BASH shell. You could export an environment variable instead, but I don't know if Java's ProcessBuilder would pick that up either.

    Having said that, I can't see why this wouldn't work:
    maudecmd[0] = "/bin/bash maude";