2

Currently I'm working on a simulation project that involve both Java and Python.

The simulator interface is written in Java Swing. The solver/engine is written in Python.

I'm using Eclipse to develop the project. I use Process and Runtime to call the Python Script with some parameters. I use pydev inside Eclipse to run the Python script.

Now, after I export the project to a JAR file, the Python code is located inside the JAR as a folder. Apparently, java won't be able to call the Python Script in the JVM anymore. Is there a way to use JAR to call Python Script and parse the simulation results?

Jython might be a option, but I'm trying to see if there is simpler way without change significant amount of code from the original Python script.

Thank you so much, William

William
  • 39
  • 2
  • 8
  • OP have you solved it at the end? I'm in the same trouble. I figured it out that when exported and launched the jar, the directory the process ('python script.py') is directly above the jar and cannot access the scripts inside it, but cannot go further. – Marco Pietrosanto Nov 23 '16 at 13:50

2 Answers2

1

Python must be configured in system path:

Process p = Runtime.getRuntime().exec("python script.py");

Or this:

ScriptEngine python = new ScriptEngineManager().getEngineByName("python");
python.eval(stringHere); # script engine runs code
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Funny story. I've spent hours on Stackoverflow trying to explain path configuration and pip setup. – Malik Brahimi Jan 21 '15 at 00:31
  • Hello Malik, thanks for the explanation. Yet, it didn't really answered my question. I did use Process p as you suggested in method one, it did work when I run through IDE (such as Eclipse). HOWEVER, when I export the whole project to a runnable JAR, the python folder and it's scripts can not be called with the normal system directory because it's in a ZIP (JAR) file. I'm trying to figure out how I would be able to call it from the JAR. – William Jan 21 '15 at 22:10
  • I know you're trying to avoid it, but I'd go with Jython. Just import Jython.jar into Eclipse, and you're good to go. – Malik Brahimi Jan 21 '15 at 22:14
  • Just a quick question to you, Malik. Let's say I package a __run__.py or main.py inside the JAR and import that in to Eclipse, do you know how can call the __run__.py file? In command line you can do a java -jar, but in Eclipse how to call that file? Thanks a lot! – William Jan 23 '15 at 23:54
0
  • Part1:(import .jar file as library in Eclipse)

You make a new project to Eclipse(name:Project1) When you open it you see JRE System Library[java version something]

1.right click on JRE System Library

2.Go->Build Path->Configure Build Path

3.You can see (Up right Corner the button[add jars or add external jars]

*Here i advise you to choose the first(add jars) but..

*First copy(or move) the Python.jar inside the project((example):Project 1)

*Now you can add it with the button(add jars).

*In this way when you finish your project the Python.jar will be imported inside the project(If you export it as a .jar from Eclipse)

..Now you can call any method of Python.jar just(import it into the class you want to use and call it)

Let me know if that works for you...

Part 2:

  • Running a file which is inside the eclipse Project(warning! before export it to jar):

1.Way(Following the path of file)

 try {
    Runtime.getRuntime().exec("cmd /c start C:/NameOfEclipseProject/Project/src/pack(Package)/script.py");

    } catch (IOException e) { e.printStackTrace(); }

2.i will add them soon..(it has been time since i used them try to find)

  • Running the script after export project to jar(warning! after export to jar)

Check this question link

Community
  • 1
  • 1
crAlexander
  • 376
  • 1
  • 2
  • 12
  • Hello crAlex, thanks for the answer, I understand how to add jar and access methods in the jar. My question here is I have package a python file in the jar, say print hello world, it's not part of the jython.jar. Through command line I can do this "java -jar jython.jar print.py" and it will call the python. Now I want to know how to do this in Eclipse...Thank you :) – William Jan 24 '15 at 19:27
  • hm.. i thought you wanted to access a method from jar file...(if i know something more i will give try)(my answer was out then.....) – crAlexander Jan 24 '15 at 21:07
  • I have done something similar before(but with an exe into the jar) one solution is to use java to copy the script somewhere(out of the jar) and then call it with (Runtime).If you dont know how to do this(copy and call from inside the jar) add a comment and i will add an extra part to the answer before – crAlexander Jan 25 '15 at 13:02
  • Hello crAlex, by copy and put the file into the jar will hide the implementation of the python script (or at least another level of security), so it is the preferred method. It would be really appreciate if you can let me know how you called the exe from the jar inside Eclipse. Thank you! – William Jan 26 '15 at 18:14
  • I haven't got it working yet, but I'm trying your second method in Part 2. try to extract it run it and delete when the Java program quits. – William Jan 27 '15 at 19:36