Run a Python script within a java action

Hi Mendix Community, as the title says I’ve been trying to run a Python script which is activated by a java action call within a Microflow. The java code works absolutely fine in Eclipse but doesn’t work when I run the actual Microflow in my app. The Microflow runs through but neither does it start the Python script nor is there an error message. But I tried it for other appllications (f.e. Notepad) and it was totally fine. I’ve already ensured that all the paths are correct. Do have any idea why this problem occurs? Or do you probably have any experience in calling a Python script from your Mendix app? Thank you in advance!   Additionally you will find the most important parts of the code. try {          String[] cmd = {<Path Python .exe>, <Path Python script .py>,<string argument>,};          Runtime.getRuntime().exec(cmd);          Core.getLogger("Call Java").info("Python Executed"); } catch (IOException e) {           Core.getLogger("Call Java").info("File [" + e.getStackTrace() + " " + e + "]"); }
2 answers

You’ll probably get burned for this kind of question, but its a super cool thing to implement and its completely possible

In terms of your java action, implement it as general as possible

You can implement this in a more general way.

  1. Set up a request handler to execute processes
  2. For both POST and GET use  url args as follows
    1. cmd: the path to the binary to execute
    2. args: space delimited params (split in java and pass to processbuilder)
    3. mimetype: to force output mimetype
  3. For POST requests, read the post body as stdin
  4. For GET, dont send in stdin as there is nothing, or make an extra url param for this if you wish
  5. In order to deal with a single param with spaces in there are also some solution involving tokenization
  6. For POST, implement internal > and >> commands for pushing stuff to the disk
  7. Add authentication!
  8. Encode all the args if you dont want people to figure out whats going on


This way can do the cool stuff local or remote with way faster testing cycles. With the request handler set up you can execute just about anything, including setting up virtualenv, pip, etc and execute shell scripts or binaries via GET or POST, even build your own programs with GCC or some other tool (which you can also build if it does not exist)


We've recently had a very similar question, which you can find here.

To summarize: ensure the program can run in your deployment environment (.exe won't run in the standard Mendix cloud, as that is hosted on Linux) and package your program and script in the resources folder.


Ockert's answer might be a nice addition, but just getting the script to run is a non-trivial step.