Обсуждение: Exception Error - Please help I've tried everything!
import java.sql.*;
public class PostgresqlClient{
public static void main(String[] arguments)
throws ClassNotFoundException, SQLException{
String driver = "org.postgresql.Driver";
String url ="localhost";
String user = "root";
String pwd = "password";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, pwd);
System.err.println("Connection complete");
con.close();
}
}
I keep getting the folowing errors:
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged (Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java :268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName (Class.java:164)
at PostgresqlClient.main(PostgresqlClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I have tried adding the postgresql.jar file to my CLASSPATH, and have also tried copying the postgresql.jar file to the same directory as the PostgresqlClient.java and PostgresqlClient.class directories. I have also tried running the following from the command line:
java -classpath postgresql.jar PostgresqlClient.class
I am using jdk1.5.0_09 and have tried using nearly all the available postgresql.jar files available.
Any advice would be most appreciated.
Cheers,
Greg Peters....
Greg: If you're running from inside NB you need to add the PG driver as a library to NB. If you have the driver on your classpath then your class should run fine outside of NB. Test and let us know if it runs outside of NB or if you have success adding the driver as a library in NB. I use Postgresql and it works beautifully. Be encouraged that if you get the right combo you'll have a superior environment! Chuck On 11/25/06, Greg Peters <gregpeters79@gmail.com> wrote: > Hello all, > > I'm trying to get the following simple program to run just to confirm a > connection to the database: > > //PostgresqlClient.java > > import java.sql.*; > > public class PostgresqlClient{ > public static void main(String[] arguments) > throws ClassNotFoundException, SQLException{ > > String driver = "org.postgresql.Driver"; > String url ="localhost"; > String user = "root"; > String pwd = "password"; > > Class.forName(driver); > > Connection con = DriverManager.getConnection(url, user, pwd); > System.err.println("Connection complete"); > > con.close(); > } > } > > > > I keep getting the folowing errors: > > Exception in thread "main" java.lang.ClassNotFoundException: > org.postgresql.Driver > at java.net.URLClassLoader$1.run(URLClassLoader.java:200) > at java.security.AccessController.doPrivileged(Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:188) > at java.lang.ClassLoader.loadClass(ClassLoader.java:306) > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) > at java.lang.ClassLoader.loadClass(ClassLoader.java:251) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) > at java.lang.Class.forName0(Native Method) > at java.lang.Class.forName(Class.java:164) > at PostgresqlClient.main(PostgresqlClient.java:12) > Java Result: 1 > BUILD SUCCESSFUL (total time: 0 seconds) > > I have tried adding the postgresql.jar file to my CLASSPATH, and have also > tried copying the postgresql.jar file to the same directory as the > PostgresqlClient.java and PostgresqlClient.class directories. I have also > tried running the following from the command line: > > java -classpath postgresql.jar PostgresqlClient.class > > I am using jdk1.5.0_09 and have tried using nearly all the available > postgresql.jar files available. > > Any advice would be most appreciated. > > Cheers, > > Greg Peters.... > >
Hi Greg, Here are a couple ideas. *. You can verify that you have a good jar file by running: jar tf postgresql.jar - org.postgresql.Driver should be listed in the jar contents. *. You're launching your app with the wrong command line: $ java -cp postgresql.jar PostgresqlClient.class Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class Use this instead: $ java -cp postgresql.jar:. PostgresqlClient Exception in thread "main" java.sql.SQLException: No suitable driver at java.sql.DriverManager.getConnection(DriverManager.java:545) at java.sql.DriverManager.getConnection(DriverManager.java:171) at PostgresqlClient.main(PostgresqlClient.java:15) Finally - that last error is because you're using an incorrectly formatted URL. Set String url = "jdbc:postgresql:littleware://localhost: 5432"; $ javac PostgresqlClient.java I don't have a 'root' user - but this will hopefully get you a little closer ... $ java -cp postgresql.jar:. PostgresqlClient Exception in thread "main" org.postgresql.util.PSQLException: FATAL: role "root" does not exist at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages (ConnectionFactoryImpl.java:443) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl (ConnectionFactoryImpl.java:98) at org.postgresql.core.ConnectionFactory.openConnection (ConnectionFactory.java:65) at org.postgresql.jdbc2.AbstractJdbc2Connection.<init> (AbstractJdbc2Connection.java:116) at org.postgresql.jdbc3.AbstractJdbc3Connection.<init> (AbstractJdbc3Connection.java:30) at org.postgresql.jdbc3.Jdbc3Connection.<init> (Jdbc3Connection.java:24) at org.postgresql.Driver.makeConnection(Driver.java:369) at org.postgresql.Driver.connect(Driver.java:245) at java.sql.DriverManager.getConnection(DriverManager.java:525) at java.sql.DriverManager.getConnection(DriverManager.java:171) at PostgresqlClient.main(PostgresqlClient.java:15) On Nov 26, 2006, at 1:48 AM, Greg Peters wrote: > Hello all, > > I'm trying to get the following simple program to run just to > confirm a connection to the database: > > //PostgresqlClient.java > import java.sql.*; > > public class PostgresqlClient{ > public static void main(String[] arguments) > throws ClassNotFoundException, SQLException{ > > String driver = "org.postgresql.Driver"; > String url ="localhost"; > String user = "root"; > String pwd = "password"; > > Class.forName(driver); > > Connection con = DriverManager.getConnection(url, user, pwd); > System.err.println("Connection complete"); > > con.close(); > } > } > > > I keep getting the folowing errors: > > Exception in thread "main" java.lang.ClassNotFoundException: > org.postgresql.Driver > at java.net.URLClassLoader$1.run(URLClassLoader.java:200) > at java.security.AccessController.doPrivileged (Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:188) > at java.lang.ClassLoader.loadClass(ClassLoader.java:306) > at sun.misc.Launcher$AppClassLoader.loadClass > (Launcher.java :268) > at java.lang.ClassLoader.loadClass(ClassLoader.java:251) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java: > 319) > at java.lang.Class.forName0(Native Method) > at java.lang.Class.forName (Class.java:164) > at PostgresqlClient.main(PostgresqlClient.java:12) > Java Result: 1 > BUILD SUCCESSFUL (total time: 0 seconds) > > I have tried adding the postgresql.jar file to my CLASSPATH, and > have also tried copying the postgresql.jar file to the same > directory as the PostgresqlClient.java and PostgresqlClient.class > directories. I have also tried running the following from the > command line: > > java -classpath postgresql.jar PostgresqlClient.class > > I am using jdk1.5.0_09 and have tried using nearly all the > available postgresql.jar files available. > > Any advice would be most appreciated. > > Cheers, > > Greg Peters.... > > > > > > >
Sorry to jump in mid-thread; I no longer read every message. But I do use NB 5.0 and 5.5 with no problem. Are you passingin -Djdbc.driver=org.postgresql.Driver to the VM? You don't in the example java -classpath etc. line below which iswhy it doesn't work. You can put it into the NB project by right-clicking the project in the project pane, going to "Properties",Run, and adding the -D command to the "extra parameters" text box. I believe that in the alternative, you can do a Class.forName("org.postgresql.Driver") in your code before you start accessingthe DB. The java.sql code doesn't know WHICH jdbc driver to load when you open the DB until you tell it. On Sun, 26 Nov 2006 15:21:58 -0800, Greg Peters <gregpeters79@gmail.com> wrote: > Chuck, > > Thanks for your input. I will see if adding the PG driver to the NB library > helps when I get home from work today. However, as I mentioned, I was unable > to get it to work from the command prompt either, even when specifically > providing a classpath with the command: > > java -classpath postgresql.jar PostgresqlClient.class (both files are in > the same directory) > > I know what you mean by psql being a good platform. I have been using it the > last few months and have been impressed. Currently all my user interfaces > have been web based using php (i see myself a semi-expert), but I am now > trying to break into the java scene (really just learning the basics). I am > ultimately going to make an interactive map, with object locations stored in > the database and a friendly gui interface - well that's the plan anyway..... > > Thanks again, > > Greg. > -- Andrew Lazarus drlaz@attglobal.net
C:\Documents and Settings\java\GregScratch\src>dir
Volume in drive C has no label.
Volume Serial Number is B468-3FBB
Directory of C:\Documents and Settings\java\GregScratch\src
27/11/2006 06:25 PM <DIR> .
27/11/2006 06:25 PM <DIR> ..
26/11/2006 10:36 PM 726 CheckString.java
24/11/2006 11:51 PM 140 helloworldapp.java
26/11/2006 05:11 PM 402,906 postgresql.jar
27/11/2006 06:25 PM 1,179 PostgresqlClient.class
27/11/2006 06:25 PM 545 PostgresqlClient.java
26/11/2006 02:49 PM 617 SetPoints.java
26/11/2006 01:44 AM 641 VolcanoApplication.java
26/11/2006 01:44 AM 432 VolcanoRobot.java
8 File(s) 407,186 bytes
2 Dir(s) 165,819,129,856 bytes free
C:\Documents and Settings\java\GregScratch\src>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C:\Documents and Settings\java\GregScratch\src>jar tf postgresql.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/postgresql/
org/postgresql/Driver$1.class
org/postgresql/Driver$ConnectThread.class
org/postgresql/Driver.class
org/postgresql/PGConnection.class
org/postgresql/PGNotification.class
org/postgresql/PGRefCursorResultSet.class
org/postgresql/PGResultSetMetaData.class
org/postgresql/PGStatement.class
org/postgresql/core/
org/postgresql/core/BaseConnection.class
org/postgresql/core/BaseResultSet.class
org/postgresql/core/BaseStatement.class
org/postgresql/core/ConnectionFactory.class
org/postgresql/core/Encoding.class
org/postgresql/core/Field.class
org/postgresql/core/Notification.class
org/postgresql/core/Oid.class
org/postgresql/core/PGBindException.class
org/postgresql/core/PGStream$1.class
org/postgresql/core/PGStream.class
org/postgresql/core/ParameterList.class
org/postgresql/core/ProtocolConnection.class
org/postgresql/core/Query.class
org/postgresql/core/QueryExecutor.class
org/postgresql/core/ResultCursor.class
org/postgresql/core/ResultHandler.class
org/postgresql/core/UTF8Encoding.class
org/postgresql/core/Utils.class
org/postgresql/core/types/
org/postgresql/core/types/PGBigDecimal.class
org/postgresql/core/types/PGBoolean.class
org/postgresql/core/types/PGByte.class
org/postgresql/core/types/PGDouble.class
org/postgresql/core/types/PGFloat.class
org/postgresql/core/types/PGInteger.class
org/postgresql/core/types/PGLong.class
org/postgresql/core/types/PGNumber.class
org/postgresql/core/types/PGShort.class
org/postgresql/core/types/PGString.class
org/postgresql/core/types/PGType.class
org/postgresql/core/types/PGUnknown.class
org/postgresql/core/v2/
org/postgresql/core/v2/ConnectionFactoryImpl$SimpleResultHandler.class
org/postgresql/core/v2/ConnectionFactoryImpl.class
org/postgresql/core/v2/FastpathParameterList.class
org/postgresql/core/v2/ProtocolConnectionImpl.class
org/postgresql/core/v2/QueryExecutorImpl$1.class
org/postgresql/core/v2/QueryExecutorImpl$2.class
org/postgresql/core/v2/QueryExecutorImpl$3.class
org/postgresql/core/v2/QueryExecutorImpl.class
org/postgresql/core/v2/SimpleParameterList.class
org/postgresql/core/v2/V2Query.class
org/postgresql/core/v3/
org/postgresql/core/v3/CompositeParameterList.class
org/postgresql/core/v3/CompositeQuery.class
org/postgresql/core/v3/ConnectionFactoryImpl$1.class
org/postgresql/core/v3/ConnectionFactoryImpl$UnsupportedProtocolException.class
org/postgresql/core/v3/ConnectionFactoryImpl.class
org/postgresql/core/v3/Portal.class
org/postgresql/core/v3/ProtocolConnectionImpl.class
org/postgresql/core/v3/QueryExecutorImpl$1.class
org/postgresql/core/v3/QueryExecutorImpl$2.class
org/postgresql/core/v3/QueryExecutorImpl$3.class
org/postgresql/core/v3/QueryExecutorImpl$ErrorTrackingResultHandler.class
org/postgresql/core/v3/QueryExecutorImpl.class
org/postgresql/core/v3/SimpleParameterList.class
org/postgresql/core/v3/SimpleQuery.class
org/postgresql/core/v3/V3ParameterList.class
org/postgresql/core/v3/V3Query.class
org/postgresql/ds/
org/postgresql/ds/PGConnectionPoolDataSource.class
org/postgresql/ds/PGPoolingDataSource$1.class
org/postgresql/ds/PGPoolingDataSource.class
org/postgresql/ds/PGSimpleDataSource.class
org/postgresql/ds/common/
org/postgresql/ds/common/BaseDataSource.class
org/postgresql/ds/common/PGObjectFactory.class
org/postgresql/ds/common/PooledConnectionImpl$ConnectionHandler.class
org/postgresql/ds/common/PooledConnectionImpl$StatementHandler.class
org/postgresql/ds/common/PooledConnectionImpl.class
org/postgresql/fastpath/
org/postgresql/fastpath/Fastpath.class
org/postgresql/fastpath/FastpathArg.class
org/postgresql/geometric/
org/postgresql/geometric/PGbox.class
org/postgresql/geometric/PGcircle.class
org/postgresql/geometric/PGline.class
org/postgresql/geometric/PGlseg.class
org/postgresql/geometric/PGpath.class
org/postgresql/geometric/PGpoint.class
org/postgresql/geometric/PGpolygon.class
org/postgresql/jdbc2/
org/postgresql/jdbc2/AbstractJdbc2Array.class
org/postgresql/jdbc2/AbstractJdbc2Blob.class
org/postgresql/jdbc2/AbstractJdbc2BlobClob$LOIterator.class
org/postgresql/jdbc2/AbstractJdbc2BlobClob.class
org/postgresql/jdbc2/AbstractJdbc2Clob.class
org/postgresql/jdbc2/AbstractJdbc2Connection$1.class
org/postgresql/jdbc2/AbstractJdbc2Connection$TransactionCommandHandler.class
org/postgresql/jdbc2/AbstractJdbc2Connection.class
org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.class
org/postgresql/jdbc2/AbstractJdbc2ResultSet$CursorResultHandler.class
org/postgresql/jdbc2/AbstractJdbc2ResultSet$NullObject.class
org/postgresql/jdbc2/AbstractJdbc2ResultSet$PrimaryKey.class
org/postgresql/jdbc2/AbstractJdbc2ResultSet.class
org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.class
org/postgresql/jdbc2/AbstractJdbc2Statement$BatchResultHandler.class
org/postgresql/jdbc2/AbstractJdbc2Statement$StatementResultHandler.class
org/postgresql/jdbc2/AbstractJdbc2Statement.class
org/postgresql/jdbc2/EscapedFunctions.class
org/postgresql/jdbc2/ResultWrapper.class
org/postgresql/jdbc2/TimestampUtils$1.class
org/postgresql/jdbc2/TimestampUtils$ParsedTimestamp.class
org/postgresql/jdbc2/TimestampUtils.class
org/postgresql/jdbc2/TypeInfoCache.class
org/postgresql/jdbc2/optional/
org/postgresql/jdbc2/optional/ConnectionPool.class
org/postgresql/jdbc2/optional/PoolingDataSource.class
org/postgresql/jdbc2/optional/SimpleDataSource.class
org/postgresql/jdbc3/
org/postgresql/jdbc3/AbstractJdbc3Blob.class
org/postgresql/jdbc3/AbstractJdbc3Clob.class
org/postgresql/jdbc3/AbstractJdbc3Connection.class
org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.class
org/postgresql/jdbc3/AbstractJdbc3ResultSet.class
org/postgresql/jdbc3/AbstractJdbc3Statement.class
org/postgresql/jdbc3/Jdbc3Array.class
org/postgresql/jdbc3/Jdbc3Blob.class
org/postgresql/jdbc3/Jdbc3CallableStatement.class
org/postgresql/jdbc3/Jdbc3Clob.class
org/postgresql/jdbc3/Jdbc3Connection.class
org/postgresql/jdbc3/Jdbc3ConnectionPool.class
org/postgresql/jdbc3/Jdbc3DatabaseMetaData.class
org/postgresql/jdbc3/Jdbc3PoolingDataSource.class
org/postgresql/jdbc3/Jdbc3PreparedStatement.class
org/postgresql/jdbc3/Jdbc3ResultSet.class
org/postgresql/jdbc3/Jdbc3ResultSetMetaData.class
org/postgresql/jdbc3/Jdbc3SimpleDataSource.class
org/postgresql/jdbc3/Jdbc3Statement.class
org/postgresql/jdbc3/PSQLParameterMetaData.class
org/postgresql/jdbc3/PSQLSavepoint.class
org/postgresql/largeobject/
org/postgresql/largeobject/BlobInputStream.class
org/postgresql/largeobject/BlobOutputStream.class
org/postgresql/largeobject/LargeObject.class
org/postgresql/largeobject/LargeObjectManager.class
org/postgresql/ssl/
org/postgresql/ssl/MakeSSL.class
org/postgresql/ssl/NonValidatingFactory$NonValidatingTM.class
org/postgresql/ssl/NonValidatingFactory.class
org/postgresql/ssl/WrappedFactory.class
org/postgresql/util/
org/postgresql/util/Base64.class
org/postgresql/util/GT.class
org/postgresql/util/MD5Digest.class
org/postgresql/util/PGInterval.class
org/postgresql/util/PGbytea.class
org/postgresql/util/PGmoney.class
org/postgresql/util/PGobject.class
org/postgresql/util/PGtokenizer.class
org/postgresql/util/PSQLDriverVersion.class
org/postgresql/util/PSQLException.class
org/postgresql/util/PSQLState.class
org/postgresql/util/PSQLWarning.class
org/postgresql/util/ServerErrorMessage.class
org/postgresql/util/StreamWrapper.class
org/postgresql/util/UnixCrypt.class
org/postgresql/xa/
org/postgresql/xa/PGXAConnection.class
org/postgresql/xa/PGXADataSource.class
org/postgresql/xa/PGXAException.class
org/postgresql/xa/RecoveredXid.class
org/postgresql/translation/
org/postgresql/translation/messages_cs.class
org/postgresql/translation/messages_de.class
org/postgresql/translation/messages_es.class
org/postgresql/translation/messages_fr.class
org/postgresql/translation/messages_it.class
org/postgresql/translation/messages_nl.class
org/postgresql/translation/messages_pl.class
org/postgresql/translation/messages_pt_BR.class
org/postgresql/translation/messages_ru.class
org/postgresql/translation/messages_tr.class
org/postgresql/translation/messages_zh_CN.class
org/postgresql/translation/messages_zh_TW.class
import java.sql.*;
public class PostgresqlClient{
public static void main(String[] Arguments)
throws ClassNotFoundException, SQLException{
String driver = "org.postgresql.Driver";
String url ="jdbc:postgresql:CSG";
String user = "root";
String pwd = "password";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, pwd);
System.err.println("Connection complete");
con.close();
}
}
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar PostgresqlClient
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient
C:\Documents and Settings\java\GregScratch\src>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This is what NB says (and I think I have added the module correctly):
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged (Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java :268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName (Class.java:164)
at PostgresqlClient.main(PostgresqlClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any ideas????
I have tried several versions of the postgresql module from the website too.
Cheers,
Greg.
Here are few guides: 1) Do not pass file name to "java". Pass fully qualified class name. Your call should be "java -cp postgresql.jar:. PostgresqlClient" 2) You can use "-verbose" to check what jar files or classes are loaded. 3) For netbeans ensure you have jar in "Project properties" -> "Libraries" Greg Peters wrote: > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > This is PostgressqlClient.java as it currently stands: > > > import java.sql.*; > > public class PostgresqlClient{ > public static void main(String[] Arguments) > throws ClassNotFoundException, SQLException{ > > String driver = "org.postgresql.Driver"; > String url ="jdbc:postgresql:CSG"; > String user = "root"; > String pwd = "password"; > > Class.forName(driver); > > Connection con = DriverManager.getConnection(url, user, pwd); > System.err.println("Connection complete"); > > con.close(); > } > } > > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > This is some of the commands I have tried and their results: > > > > C:\Documents and Settings\java\GregScratch\src>java -cp > postgresql.jar:. PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp > postgresql.jar:. PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp > postgresql.jar. PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar > PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar > PostgresqlClient > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient > > C:\Documents and Settings\java\GregScratch\src>java -cp > postgresql.jar:. PostgresqlClient > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient > > C:\Documents and Settings\java\GregScratch\src> > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > This is what NB says (and I think I have added the module correctly): > > Exception in thread "main" java.lang.ClassNotFoundException: > org.postgresql.Driver > at java.net.URLClassLoader$1.run(URLClassLoader.java:200) > at java.security.AccessController.doPrivileged (Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:188) > at java.lang.ClassLoader.loadClass(ClassLoader.java:306) > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java :268) > at java.lang.ClassLoader.loadClass(ClassLoader.java:251) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) > at java.lang.Class.forName0(Native Method) > at java.lang.Class.forName (Class.java:164) > at PostgresqlClient.main(PostgresqlClient.java:12) > Java Result: 1 > BUILD SUCCESSFUL (total time: 0 seconds) > > Any ideas???? > > I have tried several versions of the postgresql module from the > website too. > > Cheers, > > Greg. > > > > > > > >
Hi Greg! Try "java -cp .;postgresql.jar PostgresqlClient" On Windows the separator char for the classpath is ; not :. And it works for me only if the . is the first element of the classpath. Hope this helps, Sebastian Greg Peters wrote: > OK. This is driving me crazy. I have the postgresql.jar file in the same > directory. This is my directory listing: > > > C:\Documents and Settings\java\GregScratch\src>dir > Volume in drive C has no label. > Volume Serial Number is B468-3FBB > > Directory of C:\Documents and Settings\java\GregScratch\src > > 27/11/2006 06:25 PM <DIR> . > 27/11/2006 06:25 PM <DIR> .. > 26/11/2006 10:36 PM 726 CheckString.java > 24/11/2006 11:51 PM 140 helloworldapp.java > 26/11/2006 05:11 PM 402,906 postgresql.jar > 27/11/2006 06:25 PM 1,179 PostgresqlClient.class > 27/11/2006 06:25 PM 545 PostgresqlClient.java > 26/11/2006 02:49 PM 617 SetPoints.java > 26/11/2006 01:44 AM 641 VolcanoApplication.java > 26/11/2006 01:44 AM 432 VolcanoRobot.java > 8 File(s) 407,186 bytes > 2 Dir(s) 165,819,129,856 bytes free > > C:\Documents and Settings\java\GregScratch\src> > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > I tried running: > > jar tf postgresql.jar > > and got: > > C:\Documents and Settings\java\GregScratch\src>jar tf postgresql.jar > META-INF/ > META-INF/MANIFEST.MF > org/ > org/postgresql/ > org/postgresql/Driver$1.class > org/postgresql/Driver$ConnectThread.class > org/postgresql/Driver.class > org/postgresql/PGConnection.class > org/postgresql/PGNotification.class > org/postgresql/PGRefCursorResultSet.class > org/postgresql/PGResultSetMetaData.class > org/postgresql/PGStatement.class > org/postgresql/core/ > org/postgresql/core/BaseConnection.class > org/postgresql/core/BaseResultSet.class > org/postgresql/core/BaseStatement.class > org/postgresql/core/ConnectionFactory.class > org/postgresql/core/Encoding.class > org/postgresql/core/Field.class > org/postgresql/core/Notification.class > org/postgresql/core/Oid.class > org/postgresql/core/PGBindException.class > org/postgresql/core/PGStream$1.class > org/postgresql/core/PGStream.class > org/postgresql/core/ParameterList.class > org/postgresql/core/ProtocolConnection.class > org/postgresql/core/Query.class > org/postgresql/core/QueryExecutor.class > org/postgresql/core/ResultCursor.class > org/postgresql/core/ResultHandler.class > org/postgresql/core/UTF8Encoding.class > org/postgresql/core/Utils.class > org/postgresql/core/types/ > org/postgresql/core/types/PGBigDecimal.class > org/postgresql/core/types/PGBoolean.class > org/postgresql/core/types/PGByte.class > org/postgresql/core/types/PGDouble.class > org/postgresql/core/types/PGFloat.class > org/postgresql/core/types/PGInteger.class > org/postgresql/core/types/PGLong.class > org/postgresql/core/types/PGNumber.class > org/postgresql/core/types/PGShort.class > org/postgresql/core/types/PGString.class > org/postgresql/core/types/PGType.class > org/postgresql/core/types/PGUnknown.class > org/postgresql/core/v2/ > org/postgresql/core/v2/ConnectionFactoryImpl$SimpleResultHandler.class > org/postgresql/core/v2/ConnectionFactoryImpl.class > org/postgresql/core/v2/FastpathParameterList.class > org/postgresql/core/v2/ProtocolConnectionImpl.class > org/postgresql/core/v2/QueryExecutorImpl$1.class > org/postgresql/core/v2/QueryExecutorImpl$2.class > org/postgresql/core/v2/QueryExecutorImpl$3.class > org/postgresql/core/v2/QueryExecutorImpl.class > org/postgresql/core/v2/SimpleParameterList.class > org/postgresql/core/v2/V2Query.class > org/postgresql/core/v3/ > org/postgresql/core/v3/CompositeParameterList.class > org/postgresql/core/v3/CompositeQuery.class > org/postgresql/core/v3/ConnectionFactoryImpl$1.class > org/postgresql/core/v3/ConnectionFactoryImpl$UnsupportedProtocolException.class > > org/postgresql/core/v3/ConnectionFactoryImpl.class > org/postgresql/core/v3/Portal.class > org/postgresql/core/v3/ProtocolConnectionImpl.class > org/postgresql/core/v3/QueryExecutorImpl$1.class > org/postgresql/core/v3/QueryExecutorImpl$2.class > org/postgresql/core/v3/QueryExecutorImpl$3.class > org/postgresql/core/v3/QueryExecutorImpl$ErrorTrackingResultHandler.class > org/postgresql/core/v3/QueryExecutorImpl.class > org/postgresql/core/v3/SimpleParameterList.class > org/postgresql/core/v3/SimpleQuery.class > org/postgresql/core/v3/V3ParameterList.class > org/postgresql/core/v3/V3Query.class > org/postgresql/ds/ > org/postgresql/ds/PGConnectionPoolDataSource.class > org/postgresql/ds/PGPoolingDataSource$1.class > org/postgresql/ds/PGPoolingDataSource.class > org/postgresql/ds/PGSimpleDataSource.class > org/postgresql/ds/common/ > org/postgresql/ds/common/BaseDataSource.class > org/postgresql/ds/common/PGObjectFactory.class > org/postgresql/ds/common/PooledConnectionImpl$ConnectionHandler.class > org/postgresql/ds/common/PooledConnectionImpl$StatementHandler.class > org/postgresql/ds/common/PooledConnectionImpl.class > org/postgresql/fastpath/ > org/postgresql/fastpath/Fastpath.class > org/postgresql/fastpath/FastpathArg.class > org/postgresql/geometric/ > org/postgresql/geometric/PGbox.class > org/postgresql/geometric/PGcircle.class > org/postgresql/geometric/PGline.class > org/postgresql/geometric/PGlseg.class > org/postgresql/geometric/PGpath.class > org/postgresql/geometric/PGpoint.class > org/postgresql/geometric/PGpolygon.class > org/postgresql/jdbc2/ > org/postgresql/jdbc2/AbstractJdbc2Array.class > org/postgresql/jdbc2/AbstractJdbc2Blob.class > org/postgresql/jdbc2/AbstractJdbc2BlobClob$LOIterator.class > org/postgresql/jdbc2/AbstractJdbc2BlobClob.class > org/postgresql/jdbc2/AbstractJdbc2Clob.class > org/postgresql/jdbc2/AbstractJdbc2Connection$1.class > org/postgresql/jdbc2/AbstractJdbc2Connection$TransactionCommandHandler.class > > org/postgresql/jdbc2/AbstractJdbc2Connection.class > org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.class > org/postgresql/jdbc2/AbstractJdbc2ResultSet$CursorResultHandler.class > org/postgresql/jdbc2/AbstractJdbc2ResultSet$NullObject.class > org/postgresql/jdbc2/AbstractJdbc2ResultSet$PrimaryKey.class > org/postgresql/jdbc2/AbstractJdbc2ResultSet.class > org/postgresql/jdbc2/AbstractJdbc2ResultSetMetaData.class > org/postgresql/jdbc2/AbstractJdbc2Statement$BatchResultHandler.class > org/postgresql/jdbc2/AbstractJdbc2Statement$StatementResultHandler.class > org/postgresql/jdbc2/AbstractJdbc2Statement.class > org/postgresql/jdbc2/EscapedFunctions.class > org/postgresql/jdbc2/ResultWrapper.class > org/postgresql/jdbc2/TimestampUtils$1.class > org/postgresql/jdbc2/TimestampUtils$ParsedTimestamp.class > org/postgresql/jdbc2/TimestampUtils.class > org/postgresql/jdbc2/TypeInfoCache.class > org/postgresql/jdbc2/optional/ > org/postgresql/jdbc2/optional/ConnectionPool.class > org/postgresql/jdbc2/optional/PoolingDataSource.class > org/postgresql/jdbc2/optional/SimpleDataSource.class > org/postgresql/jdbc3/ > org/postgresql/jdbc3/AbstractJdbc3Blob.class > org/postgresql/jdbc3/AbstractJdbc3Clob.class > org/postgresql/jdbc3/AbstractJdbc3Connection.class > org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.class > org/postgresql/jdbc3/AbstractJdbc3ResultSet.class > org/postgresql/jdbc3/AbstractJdbc3Statement.class > org/postgresql/jdbc3/Jdbc3Array.class > org/postgresql/jdbc3/Jdbc3Blob.class > org/postgresql/jdbc3/Jdbc3CallableStatement.class > org/postgresql/jdbc3/Jdbc3Clob.class > org/postgresql/jdbc3/Jdbc3Connection.class > org/postgresql/jdbc3/Jdbc3ConnectionPool.class > org/postgresql/jdbc3/Jdbc3DatabaseMetaData.class > org/postgresql/jdbc3/Jdbc3PoolingDataSource.class > org/postgresql/jdbc3/Jdbc3PreparedStatement.class > org/postgresql/jdbc3/Jdbc3ResultSet.class > org/postgresql/jdbc3/Jdbc3ResultSetMetaData.class > org/postgresql/jdbc3/Jdbc3SimpleDataSource.class > org/postgresql/jdbc3/Jdbc3Statement.class > org/postgresql/jdbc3/PSQLParameterMetaData.class > org/postgresql/jdbc3/PSQLSavepoint.class > org/postgresql/largeobject/ > org/postgresql/largeobject/BlobInputStream.class > org/postgresql/largeobject/BlobOutputStream.class > org/postgresql/largeobject/LargeObject.class > org/postgresql/largeobject/LargeObjectManager.class > org/postgresql/ssl/ > org/postgresql/ssl/MakeSSL.class > org/postgresql/ssl/NonValidatingFactory$NonValidatingTM.class > org/postgresql/ssl/NonValidatingFactory.class > org/postgresql/ssl/WrappedFactory.class > org/postgresql/util/ > org/postgresql/util/Base64.class > org/postgresql/util/GT.class > org/postgresql/util/MD5Digest.class > org/postgresql/util/PGInterval.class > org/postgresql/util/PGbytea.class > org/postgresql/util/PGmoney.class > org/postgresql/util/PGobject.class > org/postgresql/util/PGtokenizer.class > org/postgresql/util/PSQLDriverVersion.class > org/postgresql/util/PSQLException.class > org/postgresql/util/PSQLState.class > org/postgresql/util/PSQLWarning.class > org/postgresql/util/ServerErrorMessage.class > org/postgresql/util/StreamWrapper.class > org/postgresql/util/UnixCrypt.class > org/postgresql/xa/ > org/postgresql/xa/PGXAConnection.class > org/postgresql/xa/PGXADataSource.class > org/postgresql/xa/PGXAException.class > org/postgresql/xa/RecoveredXid.class > org/postgresql/translation/ > org/postgresql/translation/messages_cs.class > org/postgresql/translation/messages_de.class > org/postgresql/translation/messages_es.class > org/postgresql/translation/messages_fr.class > org/postgresql/translation/messages_it.class > org/postgresql/translation/messages_nl.class > org/postgresql/translation/messages_pl.class > org/postgresql/translation/messages_pt_BR.class > org/postgresql/translation/messages_ru.class > org/postgresql/translation/messages_tr.class > org/postgresql/translation/messages_zh_CN.class > org/postgresql/translation/messages_zh_TW.class > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > > This is PostgressqlClient.java as it currently stands: > > > import java.sql.*; > > public class PostgresqlClient{ > public static void main(String[] Arguments) > throws ClassNotFoundException, SQLException{ > > String driver = "org.postgresql.Driver"; > String url ="jdbc:postgresql:CSG"; > String user = "root"; > String pwd = "password"; > > Class.forName(driver); > > Connection con = DriverManager.getConnection(url, user, pwd); > System.err.println("Connection complete"); > > con.close(); > } > } > > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > > This is some of the commands I have tried and their results: > > > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. > PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. > PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar. > PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar > PostgresqlClient.class > Exception in thread "main" java.lang.NoClassDefFoundError: > PostgresqlClient/class > > C:\Documents and Settings\java\GregScratch\src>java -cp > postgresql.jarPostgresqlClient > Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient > > C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. > PostgresqlClient > Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient > > C:\Documents and Settings\java\GregScratch\src> > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > This is what NB says (and I think I have added the module correctly): > > Exception in thread "main" java.lang.ClassNotFoundException: > org.postgresql.Driver > at java.net.URLClassLoader$1.run(URLClassLoader.java:200) > at java.security.AccessController.doPrivileged(Native Method) > at java.net.URLClassLoader.findClass(URLClassLoader.java:188) > at java.lang.ClassLoader.loadClass(ClassLoader.java:306) > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) > at java.lang.ClassLoader.loadClass(ClassLoader.java:251) > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) > at java.lang.Class.forName0(Native Method) > at java.lang.Class.forName(Class.java:164) > at PostgresqlClient.main(PostgresqlClient.java:12) > Java Result: 1 > BUILD SUCCESSFUL (total time: 0 seconds) > > Any ideas???? > > I have tried several versions of the postgresql module from the website > too. > > Cheers, > > Greg. >
Вложения
Hi Greg!
Try "java -cp .;postgresql.jar PostgresqlClient"
On Windows the separator char for the classpath is ; not :. And it works
for me only if the . is the first element of the classpath.
Στις Δευτέρα 27 Νοέμβριος 2006 13:29, ο/η Greg Peters έγραψε: > Thank you very much. That did it! Many thanks to all of you for your help! > I also got it to work in NB, with some fiddling. It turns out I didn't add > the library to the project properly. Thanks guys. I was just about to give > up on java and have a go at python! > > Can someone explain why the .; is necessary at all? PostgresqlClient.class lies in your current directory ("."), and adding the "." directory to the class path makes java aware of all classes in the "." "package". java will not assume you always want to have your current dir in your classpath, and hence the need for the explicit ".". > > Cheers, > > Greg > > On 11/27/06, Sebastian Esch <esch@in.tum.de> wrote: > > Hi Greg! > > > > Try "java -cp .;postgresql.jar PostgresqlClient" > > > > On Windows the separator char for the classpath is ; not :. And it works > > for me only if the . is the first element of the classpath. -- Achilleas Mantzios
Hi Greg! > Can someone explain why the .; is necessary at all? If you run java with the -cp option, java uses only the specified paths. Otherwise it assumes your classes are in the current directory ".". So you have to include "." in your classpath if that is where your class files are. For more information see http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html Sebastian