user5330618
user5330618

Reputation:

Java Spring Boot failing to find Python Script in resources folder

I am trying to call python Scripts(in resources) from my Java class. This is my code spinnet

        String res = "/Scripts/Brokers/__init__.py";

        URL pathUrl = this.getClass().getResource(res);
        String path = "";
        if(pathUrl != null)
            path = pathUrl.toString();

        ProcessBuilder pb = new ProcessBuilder("/usr/bin/python3.6", path);

ProcessBuilder is giving error No such file or directory.

P.S.

value of path = "file:/home/path-to-project/project-name/out/production/resources/Scripts/Brokers/\__init__.py"

Also how to include python scripts in jar file to run the project on linux server.

I am stuck with this issue since last 3 days. Please suggest.

Upvotes: 0

Views: 926

Answers (1)

Allan
Allan

Reputation: 12456

From java doc:

https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

Starting a new process which uses the default working directory and environment is easy:

 Process p = new ProcessBuilder("myCommand", "myArg").start();

So basically, you going to fork a sub process that will

  • start a python interpreter
  • with the python script that you have provided as argument.

The path to your script should be a normal path that your OS can understand, therefore you should not give a URI? like path (protocol:address/path).

if (path.contains(":"))
    path = (path.split(":"))[1];

Also the backslash \ before __init__.py looks suspicious.

You should be able to run ls /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/__init__.py and see the file, the same goes for ls /usr/bin/python3.6.

Upvotes: 1

Related Questions