[CLUE-Tech] Java, PostgreSQL and JDBC help needed

Jeffery Cann fabian at jefferycann.com
Fri Jun 6 05:40:26 MDT 2003


This is not a problem with the pgsql jar -- the error message reads:

ava.lang.NoClassDefFoundError: PostgreSQLTest

PostgreSQLTest is *your* class - you wrote it.  Your code defines this class.  
So, the NoClassDefFoundError means the virtual machine cannot find your 
class.  Why would this be?

You should read up on java packages.  You have the statement in your code (at 
the top of your file):

package JDBCTest;

This tells the compiler to create your PostgreSQLTest class within the package 
JDBCTest.

Like I said in my previous post, you have two ways to solve this problem:

1)  Do not create your PostgreSQLTest  class within the package JDBCTest.  You 
can do this by removing the line: 

package JDBCTest;

from your code.

2)  You can tell the compiler the proper location of your PostgreSQLTest class 
by using a different command line --- one that includes the package 
"JDBCTest" before your class:

[joe at localhost JDBCTest]# java JDBCTest.PostgreSQLTest
-------------------------------------------^^^^^^^^^^

Good luck!  When's your homework due?  ;)
Jeff

On Thursday 05 June 2003 11:12 pm, Joseph Thomas wrote:
> the driver I am trying to use is located in
> /usr/share/pgsql/pg73b1jdbc2.jar
>
> This (acording to postgresql.org) is the jdbc driver to use for postgresql
> jdbc.
>
> also, to correct my code, per the advice of another person, I have tried
> the line :
>      Class.forName("postgresql.Driver").newInstance();
> and I also tried:
>      Class.forName("org.postgresql.Driver").newInstance();
>
> here is the command line I am typing and the response:
>      [joe at localhost JDBCTest]# java PostgreSQLTest
>      Exception in thread "main" java.lang.NoClassDefFoundError:
> PostgreSQLTest
>
>
>
> ----Original Message Follows----
> From: Jeffery Cann <fabian at jefferycann.com>
> Reply-To: clue-tech at clue.denver.co.us
> To: clue-tech at clue.denver.co.us
> Subject: Re: [CLUE-Tech] Java, PostgreSQL and JDBC help needed
> Date: Thu, 5 Jun 2003 22:20:08 -0600
>
> Since you didn't tell us which class was not found, it's hard to say if
> your problem is your CLASSPATH or not.
>
> Post the command line that you are using to run your program.  My guess is
> that since you have this PostgreSQLtest class in package JDBCTest, you
> forgot
> to do this:
>
> $ java JDBCTest.PostgreSQLTest
>
> Also, make sure your path to this package is in your CLASSPATH.  A simpler
> approach is to gid rid of the package statement in your .java file and then
> do:
>
> $ java PostgreSQLTest
>
> Jeff
>
> On Thursday 05 June 2003 10:02 pm, Joseph Thomas wrote:
>  > Hello,
>  >
>  > I need some help here. I am a student and dont know alot about Linux
>  > yet.
>
> I
>
>  > am trying to be able to program in Java in linux.
>  > I am running Red Hat 9, PostgreSQL 7.3.2
>  > I have the J2SDK 1.3.1 installed and working.
>  > I have set up a database in PostgreSQL and am able to run queries on it.
>  > The trouble I am having is that I cannot get my program to connect to
>  > the DB, I am recieving a Class not found exception.
>  > I have the .jar for pgsql in my CLASSPATH set in my $HOME/.bash_profile
>
> and
>
>  > it seems to be working.
>  > Here is my code I am using:
>  >
>  > package JDBCTest;
>  > import java.sql.*;
>  >
>  > class PostgreSQLTest
>  > {
>  >   public static void main (String[] args)
>  >   {
>  > 	try
>  > 	{
>  > 	  Driver driver = (Driver)
>  > 		Class.forName("postgresql.Driver").newInstance();
>  >
>  > 	  DriverManager.registerDriver(driver);
>  >
>  > 	  String     url = "jdbc:postgresql:javatest";
>  > 	  Connection con = DriverManager.getConnection(url, "postgres", "");
>  > 	  Statement  stm = con.createStatement();
>  >
>  > 	  stm.setQueryTimeout(10);
>  > 	  ResultSet  rs  = stm.executeQuery("select col1 from test");
>  >
>  > 	  rs.next();
>  >
>  > 	  System.out.println(rs.getString(1));
>  >
>  > 	} catch (SQLException e) {
>  >
>  > 	  System.out.println("Exception!");
>  > 	  System.out.println(e.toString());
>  > 	}catch (ClassNotFoundException e) {
>  >
>  > 		  System.out.println("Exception!");
>  > 		  System.out.println(e.toString());
>  > 	}catch (InstantiationException e) {
>  >
>  > 			  System.out.println("Exception!");
>  > 			  System.out.println(e.toString());
>  > 	}	catch (IllegalAccessException e) {
>  >
>  > 			  System.out.println("Exception!");
>  > 			  System.out.println(e.toString());
>  > 	}
>  >   }//main()
>  > } //class
>  >
>  > _________________________________________________________________
>  > MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
>  > http://join.msn.com/?page=features/virus
>  >
>  > _______________________________________________
>  > CLUE-Tech mailing list
>  > CLUE-Tech at clue.denver.co.us
>  > http://clue.denver.co.us/mailman/listinfo/clue-tech

-- 
"Keep yourselves far from every form of exaggerated nationalism, racism and 
intolerance."
-- Pope John Paul II 



More information about the clue-tech mailing list