2014-01-17 84 views
0

我在使用Jython和SQLite jar文件時遇到了一些麻煩。出於某種原因,我的應用程序無法加載Sqlite JDBC類並拋出java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.sqlite.JDBC錯誤。Jython無法加載org.sqlite.JDBC /類路徑問題?

我從http://code.google.com/p/sqlitebot/downloads/detail?name=sqlitejdbc-v056.jar&

我的項目結構下載的JDBC JAR文件

/myproject的

/myproject/Lib/sqlitejdbc.jar

/myproject/jython.jar(獨立)

/myproject/Application.py

命令我使用:

$ java -jar jython.jar -Dpython.path=Lib/sqlitejdbc.jar Application.py

輸出我得到了sys.path屬性

['/Users/[User]/[ProjectLocation]/[ProjectName]', '/Users/[User]/[ProjectLocation]/[ProjectName]/Lib/sqlitejdbc.jar', '/Users/[User]/[ProjectLocation]/[ProjectName]/Lib', '/Users/[User]/[ProjectLocation]/[ProjectName]/jython.jar/Lib', '__classpath__', '__pyclasspath__/'] 

Application.py來源:

################################################################################ 
# 
# sqlite_using_jdbc - An example of using straight JDBC mechanisms to 
#      interact with a SQLite database. 
#      Creates a 'planet' table in a SQLite database 
#      named 'solarsys.db', populates it with some data and 
#      then executes a query to retrieve data from that table. 
# 
# Works with Jython 2.5, must have the zentus sqlitejdbc.jar in your 
# CLASSPATH at execution time. 
# Known to work with sqlitejdbc-v056.jar 
# 
################################################################################ 

import sys 

print sys.path 
# sys.exit(0) 

from java.lang import Class 
from java.sql import DriverManager, SQLException 

################################################################################ 

DATABASE = "solarsys.db" 
JDBC_URL = "jdbc:sqlite:%s" % DATABASE 
JDBC_DRIVER = "org.sqlite.JDBC" 

TABLE_NAME  = "planet" 
TABLE_DROPPER = "drop table if exists %s;"      % TABLE_NAME 
TABLE_CREATOR = "create table %s (name, size, solar_distance);" % TABLE_NAME 
RECORD_INSERTER = "insert into %s values (?, ?, ?);"    % TABLE_NAME 
PLANET_QUERY = """ 
select name, size, solar_distance 
from %s 
order by size, solar_distance desc 
""" % TABLE_NAME 

PLANET_DATA = [('mercury' , 'small' , 57), # distance in million kilometers 
       ('venus' , 'small' , 107), 
       ('earth' , 'small' , 150), 
       ('mars' , 'small' , 229), 
       ('jupiter' , 'large' , 777), 
       ('saturn' , 'large' , 888), 
       ('uranus' , 'medium', 2871), 
       ('neptune' , 'medium', 4496), 
       ('pluto' , 'tiny' , 5869), 
       ] 

################################################################################ 

def main(): 
    dbConn = getConnection(JDBC_URL, JDBC_DRIVER) 
    stmt = dbConn.createStatement() 
    try: 
     stmt.executeUpdate(TABLE_DROPPER) 
     stmt.executeUpdate(TABLE_CREATOR) 
    except SQLException, msg: 
     print msg 
     sys.exit(1) 

    if populateTable(dbConn, PLANET_DATA): 
     resultSet = stmt.executeQuery(PLANET_QUERY) 
     while resultSet.next(): 
      name = resultSet.getString("name") 
      size = resultSet.getString("size") 
      dist = resultSet.getInt ("solar_distance") 
      print "%-16.16s %-8.8s %4d" % (name, size, dist) 

    stmt.close() 
    dbConn.close() 
    sys.exit(0) 

################################################################################ 

def getConnection(jdbc_url, driverName): 
    """ 
     Given the name of a JDBC driver class and the url to be used 
     to connect to a database, attempt to obtain a connection to 
     the database. 
    """ 
    try: 
     Class.forName(driverName).newInstance() 
    except Exception, msg: 
     print msg 
     sys.exit(-1) 

    try: 
     dbConn = DriverManager.getConnection(jdbc_url) 
    except SQLException, msg: 
     print msg 
     sys.exit(-1) 

    return dbConn 

################################################################################ 

def populateTable(dbConn, feedstock): 
    """ 
     Given an open connection to a SQLite database and a list of tuples 
     with the data to be inserted, insert the data into the target table. 
    """ 
    try: 
     preppedStmt = dbConn.prepareStatement(RECORD_INSERTER) 
     for name, size, distance in feedstock: 
      preppedStmt.setString(1, name) 
      preppedStmt.setString(2, size) 
      preppedStmt.setInt (3, distance) 
      preppedStmt.addBatch() 
     dbConn.setAutoCommit(False) 
     preppedStmt.executeBatch() 
     dbConn.setAutoCommit(True) 
    except SQLException, msg: 
     print msg 
     return False 

    return True 

################################################################################ 
################################################################################ 

if __name__ == '__main__': 
    main() 
+1

也許與http://stackoverflow.com/q/3015059/407651有關 – mzjn

回答

0

在sqlitejdbc.jar需要的類也可以在java classpath上使用,當通過java -jar使用獨立的jython.jar時,這實際上很棘手。從Java Documentation ...

罐子

當您使用此選項,JAR文件是所有用戶類的源,和其他用戶類路徑設置將被忽略。

因此,在使用-jar時,無法向classpath添加任何內容。但是,有關解決方案,請參閱this answer - 基本上也要將jython.jar添加到類路徑中(使用-cp或CLASSPATH),然後直接運行org.python.util.jython類。