1

I want to execute a Python script (including Scipy and Numpy) from Java which should be bundled with the tomcat project by eclipse. Calling Python from Java is trivial:

Runtime.getRuntime().exec("python ...");

How can I achieve bundling the script to call it at the corresponding path? Is this even possible?

Sney
  • 2,486
  • 4
  • 32
  • 48

2 Answers2

1

You mention tomcat, so I guess your project is in a jar-file. The file may not be available to the system as a file, but only bundled in your jar, so the python interpreter can't access it. You could get the contents as a InputStream. You can find an example here:

How to get path of executed jar file

After that, you can write it to a temp file and use that file in your exec-call.

Community
  • 1
  • 1
Jacob
  • 41,721
  • 6
  • 79
  • 81
  • Actually I want to execute it out from Eclipse and out of a "war" file when distributed to the server. How do I bundle it in Eclipse? – Sney Jul 02 '11 at 03:40
1

I did some thing similar recently , i am able to run it from eclipse.

I used jython and plyjy

i kept both the jars(jython.jar and plyjy.jar) in lib folder of the Eclipse project and copied the required python files to the src folder of the project.

Then created an Python Object Instance using the following code

PySystemObjectFactory factory = new PySystemObjectFactory(InterfaceTye, moduleName,ClassName);

with the help of factory object we can create an object of the specified module and call the methods of that particular module.

I was able to achieve it only in eciplse, but i am not sure how to bundle the same for the build

suree
  • 21
  • 3
  • 2
    This is a nice solution with jython and plyjy in case you don't need NUMPY or SCIPY. This is the main reason why I would like to interface to Python. Both packages are not supported with jython. – Sney Aug 05 '11 at 18:37