I can't run a python script from java and I think it's because the script does not have execute permissions

topfoxrider :

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--
sreenivasar :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=166927&siteId=1