1

I have a spring MVC application that need to run a python script that uploads a zip to the api. I need to render the page to the user and run the python script in the background so that the zip gets updates while the user can keep on working in the Spring Application.

    Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    String[] cmd = {
                            "python",
                            "/Users/rasheenruwisha/final-year-proj/build.py",
                            "ARG 1",
                            "ARG 2",
                    };
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            t1.start();
            return modelAndView;

This is the current approach I am using, I am not getting any errors but the script does not get executed, is there anything I'm doing wrong?

2 Answers 2

2

if you use Ubuntu add "sh" and "-c":

 String[] cmd = {
                            "sh",
                            "-c",
                            "python",
                            "/Users/rasheenruwisha/final-year-proj/build.py",
                            "ARG 1",
                            "ARG 2",
                        };
                        try {
                                Runtime.getRuntime().exec(cmd);
                             } catch (IOException e) {
                                e.printStackTrace();
                             }
Sign up to request clarification or add additional context in comments.

Comments

1

1.You can also use org.python.util.PythonInterpreter

    PythonInterpreter interpreter = new PythonInterpreter();
    try {
     interpreter.execfile("/Users/rasheenruwisha/final-year-proj/build.py","ARG 1,"ARG 
     2");
    } catch (Exception e) {
        e.printStackTrace();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.