Executable jar file can't connect to remote Database.

Hello,

I'm working on a project and for this i need to communicate between a Swing App and a postgressql database.

When I execute the app within Eclipse there is no problem but when I export it to a Executable jar file i get an error:

JDBC Error:

java.lang.ClassNotFoundException: org.postgresql.Driver

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at database.DatabaseConnection.openConnection(DatabaseConnection.java:30)

at database.DatabaseLists.getPassword(DatabaseLists.java:242)

at gui.LoginFrame.actionPerformed(LoginFrame.java:146)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

etc. etc. etc.

heres the .classpath from the file:

<?xml version="1.0" encoding="UTF-8"?>

<classpath>

<classpathentry kind="src" path=""/>

<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_11"/>

<classpathentry exported="true" kind="con" path="org.eclipse.jdt.USER_LIBRARY/PostgreSQL"/>

<classpathentry exported="true" kind="con" path="org.eclipse.jdt.USER_LIBRARY/Tini"/>

<classpathentry kind="output" path=""/>

</classpath>

what am i doing wrong?

Best regards,

Guus

[1842 byte] By [Guus81nla] at [2007-11-27 6:58:05]
# 1

In addition the code of my databaseconnection class....

public class DatabaseConnection {

public String query;

public ResultSet result;

public Connection connection;

public Statement statement;

// Methode: open connectie met database

public void openConnection() {

try {

// Initialiseer de PostGreSQL-driver

Class.forName("org.postgresql.Driver");

// Syntax: "jdbc:postgresql://host:port/database"

String url = "jdbc:postgresql://<hostname>:<port>/<databasename>";

String user = "<username", pass = "<password>";

// Open connectie met database

connection = DriverManager.getConnection(url, user, pass);

// Vang JDBC-errors af

} catch( ClassNotFoundException e) {

System.err.println("JDBC Error:");

e.printStackTrace();

// Vang SQL-errors af

} catch (java.sql.SQLException e) {

System.err.println("SQL Exception:");

e.printStackTrace();

}

}

// Methode: Sluit connectie met database

public void closeConnection() {

try {

statement.close();

connection.close();

} catch( Exception e ) {

System.out.println( e.getMessage() );

}

}

// Methode: Voer een query uit

public ResultSet doQuery(String query, int getValue, Component parent) {

this.query = query;

try {

statement = connection.createStatement();

// Kijk of de statement een resultset moet teruggeven of niet

if(getValue == 1) {

result = statement.executeQuery(query);

return result;

} else {

statement.executeUpdate(query);

}

} catch (java.sql.SQLException e) {

Dialogs.databaseErrorDialog(parent);

System.err.println("SQL Exception:");

e.printStackTrace();

}

return null;

}

}

>

Guus81nla at 2007-7-12 18:48:40 > top of Java-index,Java Essentials,Java Programming...
# 2
You need to make sure the postgres driver jar is available for your exported program too and is included in the classpath.Run your application like this:java -cp yourapp.jar;postgres.jar com.package.YourMainClass
-Kayaman-a at 2007-7-12 18:48:40 > top of Java-index,Java Essentials,Java Programming...