Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating a .jar that uses a DB

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Creating a .jar that uses a DB

    Hi,

    I have written a simple server program that allows users to log in and register for a class using client.

    The server connects to a database and is run on a server at my uni. Everything worked fine until I tried to create a .jar file for the server and run it on the remote server. I get a classNotFoundException: org.postgresql.Driver I stress everything worked fine until I tried to use the jar.

    Is there something different I need to do when specifying the drivers for the DB if it is within a .jar? Like I would need to if I was including data files.

    Current DB connection code below:

    public void connect() throws Exception{
    		Class.forName("org.postgresql.Driver");
    		connect = DriverManager.getConnection("jdbc:postgresql://igor.gold.ac.uk:5432/database","username", "password");
    		st = connect.createStatement();
    		System.out.println("connected");
    		connected = true;
    	}

    Thanks for your help.
    M


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Creating a .jar that uses a DB

    Right don't worry, I have figured it out.

    Incase any one else is having a similar problem what I needed to to do was:

    1. Include a Class-Path: header in my manifest file that referenced the .jar file containing the postgresql drivers (postgresql-9.0-801.jdbc3.jar)
    2. Put a copy of postgresql-9.0-801.jdbc3.jar in the same directory on the server where my .jar is run.

    Alternatively I could have un-archived postgresql-9.0-801.jdbc3.jar, taken the .class files that are the DB drivers and added them to my .jar that I created, but this seemed a little long winded.

    well done me.

  3. The Following User Says Thank You to mycallsevern For This Useful Post:

    Json (May 12th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,327
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default Re: Creating a .jar that uses a DB

    The driver is not on your classpath, so you need to deploy it along with your app, and depending upon where you put it tell your app where the jar is.
    See PATH and CLASSPATH (The Java™ Tutorials > Essential Classes > The Platform Environment)

    Edit: got sidetracked and replied a wee bit too late. Glad you figured it out.

Similar Threads

  1. [SOLVED] Creating A Class
    By cb5950 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 31st, 2011, 07:27 AM
  2. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM
  3. Creating an Interpreter?
    By tapp13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 11th, 2010, 05:25 PM
  4. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 10:35 PM
  5. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM