1

I have been working on java app that uses python script to run some 3d visualization, it worked when I was running it from intellij but once I created jar file it just doesn't run. OS: MAC OS

How I run script: Process p1 = Runtime.getRuntime().exec("python3 vizualize3D.py");

FikiCar
  • 425
  • 2
  • 10
  • instead of `python3 visualize3D.py` do `PYTHONPATH=/path/to/your/python/project /path/to/your/interpreter/python /path/to/your/python/script.py`. E.g. if your python project is in /home/devfofikicar/pythonproject and your python interpreter is at /usr/bin/python you do `PYTHONPATH=/home/devfofikicar/pythonproject /usr/bin/python /home/devfofikicar/pythonproject/visualize3D.py` – Tin Nguyen May 27 '20 at 10:00
  • @TinNguyen my python file is inside project meaning when I run it in intellij I don't need to specify any path. So what path should I write? – FikiCar May 27 '20 at 10:05
  • You either hardcode the path (like I did in my example). Or you dynamically create an absolute path which then works on any project. Either way you need to provide the path of your working environment (`PYTHONPATH`), the path of your python interpreter (`/usr/bin/python`) and the path to the python file that is to be executed. The files have to exist somewhere in your system. Find them and for now hardcode it and see if that works. – Tin Nguyen May 27 '20 at 10:08
  • @TinNguyen ok i will do that, you one more question, how do I run it then. I mean how to I use this PYTHONPATH to run it? – FikiCar May 27 '20 at 10:12
  • Try to run your python script from terminal. Play around with it and you will get a feeling. See image: https://i.imgur.com/ylvg23N.png In this example the script only works if you specify the `PYTHONPATH`. – Tin Nguyen May 27 '20 at 10:22
  • @TinNguyen ok so I tried to do it, and it works when I run from terminal, but with that command, even in IntelliJ it doesn't work – FikiCar May 27 '20 at 10:42
  • Try: `PYTHONPATH=/path/to/your/project:$PYTHONPATH` instead of `PYTHONPATH=/path/to/your/project`. If that does not work I have no idea. It's weird that it no longer work in IntelliJ when it previously worked. – Tin Nguyen May 27 '20 at 10:50
  • @TinNguyen Still it works in the terminal but not in IntelliJ, thanks for trying maybe upvote question so someone who knows can see. – FikiCar May 27 '20 at 11:03
  • Do I understand correctly from the title of the question that the script is in the jar file? – James_D May 27 '20 at 11:05
  • @James_D yes it is – FikiCar May 27 '20 at 11:06
  • Er... No. The script is not in the jar file. The script is executed from the jar file but the script file is located on the filesystem. If that is possible then that's new to me. – Tin Nguyen May 27 '20 at 11:08
  • @TinNguyen it is in the project in IntelliJ end then I build that project into the jar, I thought that that means that the script is in a jar since all other txt files are – FikiCar May 27 '20 at 11:10
  • @TinNguyen Why would it not be possible? The jar file is just a zip archive of all the resources needed to run an application: class files along with any other resources such as images, properties files, in the case of JavaFX FXML and CSS files, etc. – James_D May 27 '20 at 11:20
  • My fault. I assumed it wasn't possible. It was new to me – Tin Nguyen May 27 '20 at 11:21

2 Answers2

2

The problem had multiple layers and solutions: 1. I didn't put .py file in jar build config 2. After putting it I always got an exception that it is null because of a typo in code 3. After trying many ways to run it this one worked Cannot run python script from java jar . The important thing is to check if you added py file to the build config and to run it in a proper way since python cannot runt files from the zip and compressed states.

FikiCar
  • 425
  • 2
  • 10
0

Assuming the script is in the jar file, you can get an input stream from the resource, and use it as the input to a Process created from the python interpreter:

// Note: the path to the script here is relative to the current class
// and follows strict resource name rules, since this is in a jar file
InputStream script = getClass().getResourceAsStream("visualize3D.py");

// The following creates a process to run python3.
// This assumes python3 is on the system path. Provide the full
// path to the python3 interpreter (e.g. /usr/bin/python3) if it's
// not on the path.

// The - option to python3 instructs it to execute a script provided
// as standard input.

Process process = new ProcessBuilder("python3", "-")
    .start() ;
OutputStream out = process.getOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while((read = script.read(buffer)) != -1) {
    pos.write(buffer, 0, read);
}
script.close();

For details on getting the correct path for the script, see How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/214750/discussion-on-answer-by-james-d-running-python-script-from-jar-file). – Samuel Liew May 27 '20 at 14:53