Why can't I access Jython access stdlib modules when called from tomcat?
I can take an application with the following code in it:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
Where is the following resources/Lib/mylib/__init__.py
::
from __future__ import print_function
from . import myfriend as thing
import os
print("Yep, everything works")
and compile it with maven by creating my-app-with-dependencies.jar
I can easily run it with java -jar my-app-with-depenendencies.jar
and it works just fine, hooray!
This is where the sad part comes in. I can put this exact code inside a Spring handler:
@RequestMapping("/doesnotwork")
public @ResponseBody String sadness() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import os");
interpreter.exec("import mylib");
return "Quoth the Java, nevermore";
}
and it doesn't magically work anymore. Nothing.
However, I can move the file from resources/Lib/
to webapp/WEB-INF/lib/Lib/
and import mylib
. But inside mylib
I can no longer import from __future__
or os
. I can import sys
, and my sys.path looks like this:
['/path/to/my/webapp/WEB-INF/lib/Lib', '__classpath__', '__pyclasspath__/']
Mine sys.path_importer_cache
looks like this:
{'__classpath__': <type 'org.python.core.JavaImporter'>,
'/path/to/my/webapp/WEB-INF/lib/Lib': None,
'/path/to/my/webapp/WEB-INF/lib/Lib/mylib': None,
'__pyclasspath__/': <ClasspathPyImporter object at 0x2>}
What am I doing wrong that I cannot import stdlib? /path/to/my/webapp/WEB-INF/lib
contains both jython-2.7-b1.jar
and jython-standalone-2.7-b1.jar
. I even tried to insert these jar files in my path and still don't dice.
I can import java classes from .jar files present in the folder, except those in jython.jars. For example, jython-2.7-b1.jar
there is org/python/apache/xml/serialize/Serializer.class
. I can import org.python
, but only exists org.python.__name__
.
source to share
No one has answered this question yet
Check out similar questions: