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.

3
  • "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? Commented May 26, 2017 at 4: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. Commented May 26, 2017 at 4: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. Commented May 26, 2017 at 5:20

1 Answer 1

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.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.