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

后端 未结 1 1293
名媛妹妹
名媛妹妹 2020-12-18 02:09

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

相关标签:
1条回答
  • 2020-12-18 02:26

    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
    0 讨论(0)
提交回复
热议问题