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--
6
  • 1
    Are you able to print a stack trace around the executed script? Is the python script located in the same directory? Commented Apr 9, 2019 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. You might also find this example helpful Commented Apr 9, 2019 at 22:41
  • myScript.py is relative. Maybe call another exec variant to give it the working directory as parameter. Commented Apr 9, 2019 at 22:43
  • You may need to adjust the working directory, again, ProcessBuilder to the rescue. You might also find this other example helpful Commented Apr 9, 2019 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)) Commented Apr 9, 2019 at 22:52

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Apparently there was an issue with my python script. Regardless this answer did help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.