1

I am new to Jython and Python, trying to build a prototype that makes use of Python code to be called from within Java. The code I am developing works in Jetty and in standalone mode (running java -jar from the command line), but not when deployed to weblogic.

How can I make weblogic(10.3.5) server/Jython recognize the Lib folder within jython-standalone-2.5.4-rc1.jar?

My Java code uses the JythonObjectFactory to invoke python modules as outlined in the Jython book: http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html

The Python modules are using external libraries like csv, logging etc. that are not packaged with jython.jar, hence I am using jython-standalone jar.

The java code includes an interface that would define the class type of the first invoked py module from within java. The interface and the input and output (to python modules) type classes are in a package structure as com.abc.xpackage. and the py modules exist at the root of this package. A controller layer calls the objectfactory and in turn executes the python code thus:

        JythonObjectFactory calFactory = new JythonObjectFactory(CalcType.class, "Calculate", "Calculate");
        CalcType engine = (CalcType)calFactory.createObject();
        output = engine.execute(input);

The entire code is bundled as a jar file which would become part of a web application deployed on weblogic. The code was compiled with maven (with jython dependencies included in the repository) and runs fine on the included Jetty runtime within eclipse.

When deployed on weblogic, however, I get a "ImportError: no module named csv" error.

To analyze what is happening, I tried printing the Jython system state path on weblogic and the standalone environment/Jetty. What I found is, on Jetty, the system path consists of the following:

C:\.m2\repo\org\python\jython\jython-standalone-2.5.3-rc1.jar\Lib, ____classpath__, ____pyclasspath__

on Weblogic, printing the system path by default shows the following:

____classpath__, ____pyclasspath__

I tried forcing the inclusion of the missing path using the code as follows:

public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {

    String pathToAppend = new File(state.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\\Lib";
    state.path.insert(0, new PyString(pathToAppend));
    state.path.append(new PyString(pathToAppend));
    System.out.println("Jython sys path: "+state.path);

Please note, I prepended as well as appended the path in different trials. The sys path on weblogic now displays the following:

Jython sys path: ['C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\wldomain\\wls135\\servers\\cgServer\\tmp\\app-1\\war\\WEB-INF\\lib\\jython-standalone-2.5.4-rc1.jar\\Lib']

I am still getting ImportError despite this forcing of sys path. Please help why this works in a local environment, and not on weblogic, and if there is any configuration I am missing. Apologize for the rambling long post, I did not know how to explain the problem better. I will try and include any code/artifacts as needed.

  • A couple of options - you could explode the jar and point to the `Lib` dir in the exploded directory. A much easier option could be do simple add the jython jar file to the top level Weblogic lib dir at `C:\\wldomain\\wls135\\lib` – Display Name is missing Nov 25 '14 at 16:51
  • @Display* Thanks for the suggestion. I tried both approaches, and the problem still persists. Please note, the Lib folder within the jython-standalone-2.5.4-rc1.jar consists of .py modules and not java classes. I copied both the jar, as well as the exploded Lib dir into the wldomain server lib directory, and explicitly pointed the paths to look at these with no luck. – NewJythonUser123 Nov 25 '14 at 19:14
  • One other idea - it could be that the weblogic server itself is also loading a jython jar onto the classpath and when you run, it's choosing that one (which will be older, before the csv module). It seems like prepending and appending would fix it but... you might want to connect to your running server with `jconsole` and have a look at the classpath. Your method looks correct based on: http://stackoverflow.com/questions/471000/jython-and-python-modules – Display Name is missing Nov 25 '14 at 20:03
  • @Display* Thanks again. I finally was able to make it work, by adding a JAVA_OPTION -Dpython.path="xxx" where xxx is the location of the aforementioned Lib, to the weblogic startup command. This made the python.path available to weblogic. The state.path.append/sys.path.append from within the program somehow never made weblogic recognize/resolve this at runtime. – NewJythonUser123 Nov 25 '14 at 20:59
  • Glad you found a solution. Feel free to post what you did as an answer and accept it so that other users know you found a solution – Display Name is missing Nov 25 '14 at 21:13

1 Answers1

0

Based on a comment(by Lassi) on the blog post below: http://www.petervannes.nl/files/e1c3c56d15d25dcfd4adb5397a9ef71e-53.php

The jython issue was resolved after explicitly adding the Lib folder python.path to the weblogic startup script as a JAVA_OPTION.

In my case I added the exploded Lib folder to the domain server lib, but based on my test this works also from within the jython jar. Both the following JAVA_OPTIONS worked:

-Dpython.path=C:\wldomain\wls135\lib\Lib
-Dpython.path=C:\wldomain\wls135\lib\jython-standalone-2.5.4-rc1.jar\Lib

The programmatic way of sys.path.append worked for the local environment(jetty) but did not seem to work for weblogic.