How can I add jars to the classpath when I invoke Jython *without* adding them to $CLASSPATH?

二次信任 提交于 2019-11-28 15:47:26

问题


I'd like to do something similar to jython -cp FOO:BAR:BAZ argle.py.

If I add FOO, BAR, and BAZ to $CLASSPATH this works. I tried to add them to sys.path at run-time, but that doesn't appear to work for jars. It does work if I add a path to the expanded jars to sys.path at runtime. Is there a simple alternative to exploding the jar files? Augmenting $CLASSPATH for every user that runs this script is not an acceptable alternative.

Thanks.


回答1:


You can use the -D option to set python.path:

jython -Dpython.path=FOO:BAR:BAZ argyle.py



回答2:


jython command supports passing arguments through to the java command... So:

jython -J-cp JAR1:JAR2

You can verify the resulting command by adding --print switch:

jython -J-cp JAR1:JAR2 --print

The above will print out the actual java command instead of executing it.




回答3:


java -cp JAR1:JAR2:jython.jar org.python.util.jython pythonScript.py works here, both on Linux and Macintosh. On Windows, swap the colons in the classpaths for semicolons and you should be golden.




回答4:


You can create a big JAR which contains all related classes. The following ant snippet shows the idea:

<target name="jar">
    <mkdir dir="build/jar"/>
    <unjar src="lib/jython.jar" dest="${classes.dir}" />
    <unjar src="lib/FOO.jar" dest="${classes.dir}" />
    <unjar src="lib/BAR.jar" dest="${classes.dir}" />
    <unjar src="lib/BAZ.jar" dest="${classes.dir}" />

    <jar destfile="build/jar/bigjython.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>


来源:https://stackoverflow.com/questions/537682/how-can-i-add-jars-to-the-classpath-when-i-invoke-jython-without-adding-them-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!