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

33,695

Solution 1

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

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

Solution 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.

Solution 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.

Solution 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>
Share:
33,695
Hank Gay
Author by

Hank Gay

I like to spend time with my lovely wife, our two beautiful daughters, and our dog. I'm also a fan of geek humor, and I've been known to flip out and write code… elegant code, if I'm really lucky.

Updated on December 22, 2020

Comments

  • Hank Gay
    Hank Gay over 3 years

    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.

  • Hank Gay
    Hank Gay about 15 years
    Now how do I run my script using bigjython.jar?
  • Horman Lewis
    Horman Lewis about 15 years
    The bigjython.jar was meant to include the original jython.jar plus your extra jars. So at the end the bigjython jar should work the same as jython.jar but without the need of the -cp parameter.
  • Hank Gay
    Hank Gay about 15 years
    I'm not using jython.jar - I'm using jython command from the shell to execute argle.py
  • monojohnny
    monojohnny over 12 years
    NB: on Windows Replace colon ':' separator with semi-colon ';' separator. jython -Dpython.path=FOO;BAR;BAZ argyle.py
  • Paolo
    Paolo about 11 years
    on Windows use the double backslash \\ when specifying paths, and if the path name contains spaces surround it with double quotes. Don't know if the trailing slash / is required. I get things work without it.