3

I have a standard Maven project and I want to run the meTypeset script. This script takes 3 args where the second one is a file and the third one is a folder created as output.

This is how the script runs in a cmd:

meTypeset.py docx <input> <output_folder> [options]

This is how I try to run it in Java:

public static void main(String args[]) {
    String[] cmd = {
            "python",
            "resources\\pyscripts\\meTypeset.py",
            "docx",
            "resources\\exampledocs\\example_journal.docx",
            "resources\\output"
    };
    try {
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Nothing happens, no errors but no result also

Iraklis Bekiaris
  • 1,163
  • 15
  • 43

2 Answers2

3

Unlike python Java may need some help. Do I guess correctly you are running on Windows?

You invoke the Runtime.exec() method. The method returns a Process instance, and in it's documentation you can read

By default, the created process does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the process. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the process may cause the process to block, or even deadlock.

So it is likely your process is started by the OS but gets blocked due to I/O restrictions. Get around that by reading the STDOUT and STDERR streams until your process finishes. One good programming model is visible at https://www.baeldung.com/run-shell-command-in-java

Queeg
  • 7,748
  • 1
  • 16
  • 42
3

@Hiran Chaudhuri explained the error correctly. I am just posting how I solved it, thanks to @ Sonnenhut comment.

    Runtime rt = Runtime.getRuntime();
    String[] commands = {
            "python",
            "src\\main\\resources/pyscripts/meTypeset.py",
            "docx",
            "src\\main\\resources/exampledocs/example_journal.docx",
            "src\\main\\resources/output"
    };
    Process proc = rt.exec(commands);

    BufferedReader stdInput = new BufferedReader(new
            InputStreamReader(proc.getInputStream()));

    BufferedReader stdError = new BufferedReader(new
            InputStreamReader(proc.getErrorStream()));

// Read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

// Read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    }
Iraklis Bekiaris
  • 1,163
  • 15
  • 43