0

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.

3
  • Can you change the value of path to /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/\__init__.py? without the protocol part, and try again? Commented Mar 14, 2019 at 7:05
  • 1
    There is a extra back-slash in the value of path. please remove it and try. Commented Mar 14, 2019 at 7:18
  • Sorry! It was for formatting purpose of question. Actual Path was without backslash. Commented Mar 14, 2019 at 8:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes I tried this. It worked on my local. But when I build jar file and ran it. Again it started giving "FileNotFoundException" . How to access python and csv files in resources in jar ?
@AwesomeBlossom: where do you run your jar file? on the same machine on or on a server?
On linux Server. Path value is like -- "/way-to-jar/data-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/Scripts/Brokers/__init__.py" on server. It is searching in classes. but file is present in resources folder
can you do a ssh connection on this server and run: ls /home/path-to-project/project-name/out/production/resources/Scripts/Brokers/__init__.py and ls /usr/bin/python3.6 and tell me the result of both commands?
Have you check that the user you are using to run the jar file has read access to the __init__.py and read/execution access on the python?
|

Your Answer

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