metamemelord
metamemelord

Reputation: 528

Running a Python script from Spring-Boot Web Application on embedded Tomcat

I am trying to make a web-app that takes input from the user using HTML Form on Spring-boot (Which is running on embedded Tomcat); I need to pass this input to a Python script sitting somewhere on my hard disk as command-line arg.

Here is my controller that fetches the data from HTML form (Getting data from the form is working fine.)

@Controller
public class PortalController {
    @RequestMapping(value="")
    public String hello(){
        return "portal/welcome";
    }
    @RequestMapping(value="",method=RequestMethod.POST)
    public String addAObjectForm(@RequestParam String val) throws IOException {
        String cmd = "python Fetch.py \""+val+"\"";
        System.out.println(cmd);
        Runtime.getRuntime().exec(cmd);
        return "redirect:/";
    }
}

The variable 'cmd' stores this value finally: python Fetch.py "Command-line-arg-from-html"

The Python code is scraping web. The code is so big that it cannot be ported to Java. And my v. sweet teacher has asked me to make the web-app on Spring-Boot. Since the app is running on Tomcat, it doesn't look like I will be able to execute the Py script directly as if it were on console. Can someone suggest a way of doing it, please? Any bit of help is highly appreciated.

Note: Both, Spring-Boot Project and Python Script, are working standalone without issues.

The issue I am having is, the code just prints the variable 'cmd' on the console and continues. No changes are reflected in the database (which are supposed to be done by the Py script), it indicates that the script did not execute. Here is the console window:

2018-03-03 05:44:47.733  INFO 10752 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-03 05:44:47.771  INFO 10752 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-03-03 05:44:47.771  INFO 10752 --- [  restartedMain] org.OpenLyrics.Portal.PortalApplication  : Started PortalApplication in 3.161 seconds (JVM running for 7.414)
2018-03-03 05:44:55.737  INFO 10752 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-03-03 05:44:55.738  INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-03-03 05:44:55.765  INFO 10752 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms
python Fetch.py "Argument 1"
python Fetch.py "Argument 2"
python Fetch.py "Argument 3"

Upvotes: 6

Views: 23648

Answers (1)

metamemelord
metamemelord

Reputation: 528

I dug the internet a lot and found the solution.

One can do this using the following commands:

String fetching = "python " + "c:\\Fetch.py \"" + songDetails + "\"";
String[] commandToExecute = new String[]{"cmd.exe", "/c", fetching};
Runtime.getRuntime().exec(commandToExecute);

Upvotes: 11

Related Questions