specifying classpath for standalone jython

2019-01-26 20:38发布

问题:

I am trying to run a python script via jython on a server that I won't be able to install anything.

I can run

java -jar jython.jar

and that works fine. I am unable to use mm.mysql-2.0.14 driver because it can't find the jar. I set the classpath to include it, as I did on a test system with a standard (that is, non-standalone) jython install. It still says it can't find the jar.

I have tried things like:

export CLASSPATH=/tmp/mm.mysql-2.0.14-bin.jar:/tmp/zxJDBC.jar; java -jar jython.jar

and

java -cp /tmp/mm.mysql-2.0.14-bin.jar  -jar jython.jar

no dice.

still the error: zxJDBC.DatabaseError: driver [org.gjt.mm.mysql.Driver] not found

回答1:

In my Windows environment I was able to run:

java -cp c:\jars\ojdbc6.jar;c:\jython2.5.2\jython.jar org.python.util.jython schema_ora.py 

where schema_ora.py is a script I want to run which uses Oracle JDBC driver.

I also tried it on Linux with:

[mn@test-db mn]# export CLASSPATH=/usr/local/jars/ojdbc6.jar; /usr/local/jython2.5.2/bin/jython -J-Xmx4048m schema_ora.py jdbc:oracle:thin:@192.168.19.128:1521:testdb usr passwd

So it is possible to run script that way.

As for your problem maybe you do not have rights to see MySQL JDBC driver? I assume you use Linux, so you can check if you can see that file:

file /tmp/mm.mysql-2.0.14-bin.jar

I tried it with Oracle driver on my Linux box:

[mn@test-db mn]# file /usr/local/jars/ojdbc6.jar
/usr/local/jars/ojdbc6.jar: Zip archive data, at least v2.0 to extract


回答2:

From the Java Documentation ...

-jar

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

So it it not possible to add anything to the classpath when using -jar. You'd either have to repackage the jython.jar to include the required classes, or preferably use Michał Niklas' solution of adding the jython.jar to the classpath (either using -cp or CLASSPATH) and running the org.python.util.jython class directly.



标签: mysql jython