How do launch an external program on ubuntu shell (WSL) from Java on Windows

1

I'm on Windows, and i try to work on a Java application that was written to be use on a Linux OS, because the program will launch some shell script at some point.

I have WSL (Windows Subsystem for Linux, also know as Ubuntu bash), so executing shell script should not be a problem, but i have an error : 0x80070057

The code that launch the external process :

public Process startProcess(List<String> commands ) throws IOException {
    ProcessBuilder etProcessBuilder= new ProcessBuilder(commands);
    Process etProcess = etProcessBuilder.start();
    ProcessOutputReader stdReader= new ProcessOutputReader(etProcess.getInputStream(), LOGGER::info);
    ProcessOutputReader errReader= new ProcessOutputReader(etProcess.getErrorStream(), LOGGER::error);
    new Thread(stdReader).start();
    new Thread(errReader).start();
    return etProcess;
}

The commands param are set with with something like this :

  • "/mnt/d/some/path/scripts/initEAF.sh"
  • "-argForTheScript"
  • "some value"
  • "-anotherArg"
  • "other value"

I also tried to add "bash.exe" as first command, but it doesn't seems to work.

The ProcessOutputReaderis a class to log the stream from the process

class ProcessOutputReader implements Runnable {
    private final InputStream inputStream;
    private Consumer<String> loggingFunction;

    ProcessOutputReader(InputStream inputStream, Consumer<String> loggingFunction) {
        this.inputStream = inputStream;
        this.loggingFunction = loggingFunction;
    }

    private BufferedReader getBufferedReader(InputStream is) {
        return new BufferedReader(new InputStreamReader(is));
    }

    @Override
    public void run() {
        BufferedReader br = getBufferedReader(inputStream);
        String ligne;
        try {
            while ((ligne = br.readLine()) != null) {
                loggingFunction.accept(ligne);
            }
        } catch (IOException e) {
            LOGGER.error("Error occur while reading the output of process ", e);
        }
    }
}

Any idea is welcome.

java
windows-subsystem-for-linux
asked on Stack Overflow Sep 22, 2017 by J.Mengelle

1 Answer

0

*.sh is not an executable file. You need run it by a shell, such as bash xxx.sh -args or sh xxx.sh -args if your java app run inside wsl.

If your java app run on Windows, it should be bash.exe -c xxx.sh

answered on Stack Overflow Oct 26, 2017 by reker

User contributions licensed under CC BY-SA 3.0