Thread: java.lang.ClassNotFoundException loading JDBC driver
Hi, Ik know this is a FAQ, but this looks different. While loading the pgsql jdbc driver (using Class.forName() as the documentation suggests) I get a ClassNotFoundException: Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver even though the JAR file _is_ on my classpath. I've tried both the CLASSPATH environment variable, and the -cp command line argument. The test app I use is extremely bare bones, uses nothing beyond JDBC, and only tries to load the driver and establish a connection. If I try to execute the class org.postgresql.Driver directly, with the same classpath specification, the class is found, and I get (as expected) an error message saying the class has no main method. This suggests that my classpath is okay. If I move the JAR file into the JVM's lib/ext directory, things work perfectly. I need to be able to use the driver without doing that, however. What am I doing wrong? I'm using the pg73jdbc3.jar from jdbc.postgresql.org, downloaded today. My JVM is j2sdk1.4.1_01 from Sun, on Red Hat Linux 8.0 I also tried the 7.2, 7.1 and 7.0 drivers, and JVM jdk1.3.1_06 from Sun. (I used pg73jdbc2.jar instead of pg73jdbc3.jar there) My test app is very simple: import java.sql.*; public class TestApp { public static void main( String argv[] ) throws java.sql.SQLException, java.lang.ClassNotFoundException { Class.forName( "org.postgresql.Driver" ); String url = "jdbc:postgresql://dbhost:5432/test"; Connection conn = DriverManager.getConnection( url, "test", "xxxxxxxx" ); } } Ernst Jan
Ernst, try running with java -d to show debugging information, it will show you the classpath it is using Dave On Sun, 2003-02-09 at 16:27, Ernst Jan Plugge wrote: > Hi, > > > Ik know this is a FAQ, but this looks different. > > While loading the pgsql jdbc driver (using Class.forName() as the > documentation suggests) I get a ClassNotFoundException: > > Exception in thread "main" java.lang.ClassNotFoundException: > org.postgresql.Driver > > even though the JAR file _is_ on my classpath. I've tried both the > CLASSPATH environment variable, and the -cp command line argument. The > test app I use is extremely bare bones, uses nothing beyond JDBC, and only > tries to load the driver and establish a connection. > > If I try to execute the class org.postgresql.Driver directly, with the > same classpath specification, the class is found, and I get (as expected) > an error message saying the class has no main method. This suggests that > my classpath is okay. > > If I move the JAR file into the JVM's lib/ext directory, things work > perfectly. I need to be able to use the driver without doing that, > however. What am I doing wrong? > > I'm using the pg73jdbc3.jar from jdbc.postgresql.org, downloaded today. > My JVM is j2sdk1.4.1_01 from Sun, on Red Hat Linux 8.0 > > I also tried the 7.2, 7.1 and 7.0 drivers, and JVM jdk1.3.1_06 from Sun. > (I used pg73jdbc2.jar instead of pg73jdbc3.jar there) > > My test app is very simple: > > import java.sql.*; > > public class TestApp > { > public static void main( String argv[] ) throws > java.sql.SQLException, java.lang.ClassNotFoundException > { > Class.forName( "org.postgresql.Driver" ); > String url = "jdbc:postgresql://dbhost:5432/test"; > Connection conn = DriverManager.getConnection( url, "test", "xxxxxxxx" ); > } > } > > > Ernst Jan > > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org -- Dave Cramer <Dave@micro-automation.net>
On 10 Feb 2003, Dave Cramer wrote: > Ernst, > > try running with java -d to show debugging information, it will show you > the classpath it is using That option isn't recognized by the JVM I'm using. Anyway, my problem's been solved by another reader. Note to self: Rule #1: Just because you've read the docs, doesn't mean you actually know everything in there. Rule #2: The following two are _not_ equivalent: export CLASSPATH=.:foo.jar java -jar bar.jar export CLASSPATH=.:foo.jar:bar.jar java MainClass Where MainClass is the main class in bar.jar. I'm not yet 100% confident on the semantics of class loading, but I know a little more than I did yesterday, which is always a good thing :-) Thanks. Ernst Jan > > Dave > On Sun, 2003-02-09 at 16:27, Ernst Jan Plugge wrote: > > Hi, > > > > > > Ik know this is a FAQ, but this looks different. > > > > While loading the pgsql jdbc driver (using Class.forName() as the > > documentation suggests) I get a ClassNotFoundException: > > > > Exception in thread "main" java.lang.ClassNotFoundException: > > org.postgresql.Driver > > > > even though the JAR file _is_ on my classpath. I've tried both the > > CLASSPATH environment variable, and the -cp command line argument. The > > test app I use is extremely bare bones, uses nothing beyond JDBC, and only > > tries to load the driver and establish a connection. > > > > If I try to execute the class org.postgresql.Driver directly, with the > > same classpath specification, the class is found, and I get (as expected) > > an error message saying the class has no main method. This suggests that > > my classpath is okay. > > > > If I move the JAR file into the JVM's lib/ext directory, things work > > perfectly. I need to be able to use the driver without doing that, > > however. What am I doing wrong? > > > > I'm using the pg73jdbc3.jar from jdbc.postgresql.org, downloaded today. > > My JVM is j2sdk1.4.1_01 from Sun, on Red Hat Linux 8.0 > > > > I also tried the 7.2, 7.1 and 7.0 drivers, and JVM jdk1.3.1_06 from Sun. > > (I used pg73jdbc2.jar instead of pg73jdbc3.jar there) > > > > My test app is very simple: > > > > import java.sql.*; > > > > public class TestApp > > { > > public static void main( String argv[] ) throws > > java.sql.SQLException, java.lang.ClassNotFoundException > > { > > Class.forName( "org.postgresql.Driver" ); > > String url = "jdbc:postgresql://dbhost:5432/test"; > > Connection conn = DriverManager.getConnection( url, "test", "xxxxxxxx" ); > > } > > } > > > > > > Ernst Jan > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org >
Ernst, When you specify a jar then the classpath environment variable is NOT used. Dave On Mon, 2003-02-10 at 16:34, Ernst Jan Plugge wrote: > On 10 Feb 2003, Dave Cramer wrote: > > > Ernst, > > > > try running with java -d to show debugging information, it will show you > > the classpath it is using > > That option isn't recognized by the JVM I'm using. > > > Anyway, my problem's been solved by another reader. Note to self: > > Rule #1: Just because you've read the docs, doesn't mean you actually know > everything in there. > > Rule #2: The following two are _not_ equivalent: > > export CLASSPATH=.:foo.jar > java -jar bar.jar > > export CLASSPATH=.:foo.jar:bar.jar > java MainClass > > Where MainClass is the main class in bar.jar. > > > I'm not yet 100% confident on the semantics of class loading, but I know a > little more than I did yesterday, which is always a good thing :-) > > Thanks. > > > Ernst Jan > > > > > > Dave > > On Sun, 2003-02-09 at 16:27, Ernst Jan Plugge wrote: > > > Hi, > > > > > > > > > Ik know this is a FAQ, but this looks different. > > > > > > While loading the pgsql jdbc driver (using Class.forName() as the > > > documentation suggests) I get a ClassNotFoundException: > > > > > > Exception in thread "main" java.lang.ClassNotFoundException: > > > org.postgresql.Driver > > > > > > even though the JAR file _is_ on my classpath. I've tried both the > > > CLASSPATH environment variable, and the -cp command line argument. The > > > test app I use is extremely bare bones, uses nothing beyond JDBC, and only > > > tries to load the driver and establish a connection. > > > > > > If I try to execute the class org.postgresql.Driver directly, with the > > > same classpath specification, the class is found, and I get (as expected) > > > an error message saying the class has no main method. This suggests that > > > my classpath is okay. > > > > > > If I move the JAR file into the JVM's lib/ext directory, things work > > > perfectly. I need to be able to use the driver without doing that, > > > however. What am I doing wrong? > > > > > > I'm using the pg73jdbc3.jar from jdbc.postgresql.org, downloaded today. > > > My JVM is j2sdk1.4.1_01 from Sun, on Red Hat Linux 8.0 > > > > > > I also tried the 7.2, 7.1 and 7.0 drivers, and JVM jdk1.3.1_06 from Sun. > > > (I used pg73jdbc2.jar instead of pg73jdbc3.jar there) > > > > > > My test app is very simple: > > > > > > import java.sql.*; > > > > > > public class TestApp > > > { > > > public static void main( String argv[] ) throws > > > java.sql.SQLException, java.lang.ClassNotFoundException > > > { > > > Class.forName( "org.postgresql.Driver" ); > > > String url = "jdbc:postgresql://dbhost:5432/test"; > > > Connection conn = DriverManager.getConnection( url, "test", "xxxxxxxx" ); > > > } > > > } > > > > > > > > > Ernst Jan > > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > > > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > > -- Dave Cramer <Dave@micro-automation.net>