2

I'm trying to run a python script whenever a button on my gui (swing) is pressed. However, the script never runs and I'm not sure how to fix this. I know the script works fine independently, it should be py not python because windows, and my file system ntfs.

So far I've been trying to use code that can be summarized as below:

myBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          Process p = Runtime.getRuntime().exec("py myScript.py");

        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
      }
});

I don't think I can chmod ntfs stuff but I tried setting permissions via right clicking the python file and trying to mess with the security settings. Full control for the script to users does nothing.

The python script has the following permissions, my guess is my code isn't working because it does not have execute permissions.

-rw-r--r--
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
topfoxrider
  • 143
  • 10
  • 1
    Are you able to print a stack trace around the executed script? Is the python script located in the same directory? – Sterling Archer Apr 09 '19 at 22:40
  • 2
    The "script" itself doesn't need to be executable, as Python will load it and execute it self. I, personally, wouldn't use `Runtime.exec` and would instead use `ProcessBuilder`, it's more configurable. You should also make sure you're consuming the output and error streams, [for example](https://stackoverflow.com/questions/21081898/how-to-run-a-python-file-from-java-using-an-absolute-path/21081924#21081924). You might also find [this example](https://stackoverflow.com/questions/19102989/java-is-there-a-way-to-run-a-system-command-and-print-the-output-during-executi/19103198#19103198) helpful – MadProgrammer Apr 09 '19 at 22:41
  • `myScript.py` is relative. Maybe call another `exec` variant to give it the working directory as parameter. – Michael Butscher Apr 09 '19 at 22:43
  • You may need to adjust the working directory, again, `ProcessBuilder` to the rescue. You might also [find this other example](https://stackoverflow.com/questions/18838904/java-program-that-runs-commands-with-linux-terminal/18839348#18839348) helpful – MadProgrammer Apr 09 '19 at 22:44
  • maybe you should have a look at the standard output/error of the process, eventually also provide a standard input... also try if `py` itself is being called (with a very simple script, e.g. writing actual time to a file) ((it normally is not a good idea to do long time processing on the EDT)) – user85421 Apr 09 '19 at 22:52
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also break a `String arg` into `String[] args` to account for things like paths containing space characters. – Andrew Thompson Apr 10 '19 at 01:18

1 Answers1

0

Use complete python executable path instead of "py". It executes the file with just read permissions.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Sample {

    public static void main(String[] args) throws Exception {
        try {
            Process p = Runtime.getRuntime().exec("C:/Windows/py myScript.py");
            String cmdOutput = null;
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            // read the output from the command
            while ((cmdOutput = stdInput.readLine()) != null) {
                System.out.println(cmdOutput);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

myScript.py

print("This line will be printed.")

Output:

C:\Users\Administrator\Documents\demo>javac Sample.java

C:\Users\Administrator\Documents\demo>java Sample
This line will be printed.