1

I am working on a Java application in which I have a Jython script written to perform a task.The code works fine and the Jython script executes when it is executed through Eclipse IDE. But when I export the Java application to .jar file the Jython script doesn't run.

This is the directory structure I am following in Eclipse IDE:

application.jar
   |-- com
       |--example

           |-- package1
             |-- Function2.Java
             |-- pre_myAction.py

           |-- package2
             |-- Function1.Java

I am trying to call the script_function from the Function1.java in the following way:

PythonInterpreter interp = new PythonInterpreter();
interp.execfile(".//com//example//pre_myAction.py");
String funcName = "function1";
PyObject someFunc = interp.get(funcName);

if (someFunc == null) {
  throw new Exception("Could not find Python function: " + funcName);
}

try {
   interp.set("DataMap", dataMap);
   someFunc.__call__(new PyString(file1));
  } catch (PyException e1) {
    LOGGER.log(Level.SEVERE, e1.getMessage());
}

interp.cleanup();
interp.close();

When I tried to execute the jar from the command prompt I am getting this error:

File not found -//com//example//pre_myAction.py

And the same code gets executed in Eclipse IDE without any error.

Can anyone provide the solution or suggestion on how to execute the jython script with in a jar file.

prasanth vaidya
  • 21
  • 2
  • 10
  • "doesn't run" doesn't give us much to go on. What's the error message? How are you invoking the Jar? Have you been able to run simpler Jython scripts the same way? – dimo414 May 26 '17 at 04:24
  • When you run that jar on the command line - did you enable java for jython? Btw: your code "example" doesn't contain any code. – GhostCat May 26 '17 at 04:25
  • It looks like pre_myAction.py is in com/example/package1, not com/example. Why that would work in Eclipse IDK. EDIT: Never mind, I think I see it. – jaxad0127 May 26 '17 at 05:20

1 Answers1

0

A quick browser through the Jython code and it looks like PythonInterpreter.execfile(String) will look for the file in the filesystem, not the JAR. You can try looking it up using Class.getResourceAsStream and using the InputStream version of the method.

It looks like Jython will load files from the root of the JAR automatically, which gives behavior mirroring CPython. See Bundling python files inside jar for access via jython for details.

jaxad0127
  • 111
  • 1
  • 5