我已经构建了一个简单的应用程序,它使用java持久性实体管理器连接在derby(Netbeans)中生成的数据库,该数据库与jTable绑定。问题是,当我在其他系统上运行.jar文件时,它不会执行。谁能告诉我为什么会这样?
在其他系统上运行时出错:
C:\>java -jar persist.jar
[EL Info]: 2013-01-08 17:06:45.715--ServerSession(4597467)--EclipseLink, version
: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Severe]: 2013-01-08 17:06:48.24--ServerSession(4597467)--Exception [EclipseL
ink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.p
ersistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.Connec
tException : Error connecting to server localhost on port 1527 with message Conn
ection refused: connect.
Error Code: 40000
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: E
xception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r104
61): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.Connec
tException : Error connecting to server localhost on port 1527 with message Conn
ection refused: connect.
Error Code: 40000
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(En
tityManagerSetupImpl.java:517)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.get
DatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.cre
ateEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createE
ntityManagerImpl(EntityManagerFactoryImpl.java:294)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createE
ntityManager(EntityManagerFactoryImpl.java:272)
at persist.NewJFrame.initComponents(NewJFrame.java:30)
at persist.NewJFrame.<init>(NewJFrame.java:17)
at persist.NewJFrame$1.run(NewJFrame.java:101)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v2
0111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.Connec
tException : Error connecting to server localhost on port 1527 with message Conn
ection refused: connect.
Error Code: 40000
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(Dat
abaseException.java:324)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(Dat
abaseException.java:319)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConn
ector.java:138)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(
DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAn
dDetectDatasource(DatabaseSessionImpl.java:584)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.log
in(EntityManagerFactoryProvider.java:206)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(En
tityManagerSetupImpl.java:488)
... 21 more
Caused by: java.sql.SQLNonTransientConnectionException: java.net.ConnectExceptio
n : Error connecting to server localhost on port 1527 with message Connection re
fused: connect.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unkn
own Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Sourc
e)
at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConn
ector.java:98)
... 25 more
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectExcep
tion : Error connecting to server localhost on port 1527 with message Connection
refused: connect.
at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
at org.apache.derby.client.am.Connection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConne
ction(Unknown Source)
... 29 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at javax.net.DefaultSocketFactory.createSocket(Unknown Source)
at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 35 more
答案 0 :(得分:1)
看看this SO question。看起来像一个类似的问题(即,依赖jar可能不在classpath
)。
修改强>
由于您发布了堆栈跟踪,现在很明显您遇到了一些数据库连接问题:
java.net.ConnectException : Error connecting to server localhost on port 1527 with messageConnection refused: connect.
您是否检查过数据库是否已启动&amp;在其他系统上正常运行?
答案 1 :(得分:1)
检查persistence.xml中的数据库URL
<properties>
<property name="javax.persistence.jdbc.password" value="sa"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://??:1527/??"/>
</properties>
当您在创建数据库的netbeans中运行应用程序时,Netbeans会分配默认位置。我建议你传递url作为创建实体管理器工厂的参数,或者将它放在属性文件中并加载它。
java -jar project.jar persistence.properties