7

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

3
  • what are the contents of scriptName and scriptStr? Commented Mar 31, 2010 at 8:01
  • In the jar file. Its on the classpath when I invoke jython. If I print sys.path you see it there. Just to be thorough I've tried adding the jar directly to the code using sys.path.append() but it did not make any difference. Commented Mar 31, 2010 at 8:12
  • Look at zipimport (docs.python.org/library/zipimport.html) and at the example at the bottom. When you write init.py, do mean __init__.py, right? Commented Mar 31, 2010 at 9:15

1 Answer 1

5

the following works for me :

import sys
import os

import java.lang.ClassLoader 
import java.io.InputStreamReader
import java.io.BufferedReader

loader = java.lang.ClassLoader.getSystemClassLoader()
stream = loader.getResourceAsStream("com/example/action/myAction.py")
reader = java.io.BufferedReader(java.io.InputStreamReader(stream))

script = ""                          
line = reader.readLine()
while (line != None) : 
    script += line + "\n"
    line = reader.readLine()

exec(script)
  1. Loading the Script from the ClassPath as a String in 'script'
  2. exec the script with exec
Sign up to request clarification or add additional context in comments.

2 Comments

That is a really good answer. I was hoping for something a bit simpler, but I think that this is likely to be the best option.
hello, could you please add how should we run this script. Thank you

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.