How do you invoke a python script inside a jar file using python ?

Posted by Trevor on Stack Overflow See other posts from Stack Overflow or by Trevor
Published on 2010-03-31T07:56:24Z Indexed on 2010/03/31 10:53 UTC
Read the original article Hit count: 455

Filed under:
|
|
|
|

I'm working on an application that intersperses a bunch of jython and java code. Due to the nature of the program (using wsadmin) we are really restricted to Python 2.1

We currently have a jar containing both java source and .py modules. The code is currently invoked using java, but I'd like to remove this in favor of migrating as much functionality as possible to jython.

The problem I have is that I want to either import or execute python modules inside the existing jar file from a calling jython script. I've tried a couple of different ways without success.

My directory structure looks like:

application.jar
  |-- com
       |--example
            |-- action
                 |-- MyAction.class
                 |-- pre_myAction.py

The 1st approach I tried was to do imports from the jar. I added the jar to my sys.path and tried to import the module using both import com.example.action.myAction and import myAction. No success however, even when I put init.py files into the directory at each level.

The 2nd approach I tried was to load the resource using the java class. So I wrote the below code:

import sys
import os
import com.example.action.MyAction as MyAction

scriptName = str(MyAction.getResource('/com/example/action/myAction.py'))
scriptStr = MyAction.getResourceAsStream('/com/example/action/myAction.py')

try:
  print execfile(scriptStr) 
except:
  print "failed 1"

try:
  print execfile(scriptName) 
except:
  print "failed 2"

Both of these failed. I'm at a bit of a loss now as to how I should proceed. Any ideas ?

cheers,

Trevor

© Stack Overflow or respective owner

Related posts about python

Related posts about jython