aspose file tools*
The moose likes JDBC and the fly likes JDBC not included into jar file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC not included into jar file?" Watch "JDBC not included into jar file?" New topic
Author

JDBC not included into jar file?

Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
Hi there, was wondering if anyone could help me.

I developed an application which works with MySQL using JDBC. It all works fine, but when I try to create a JAR file for my program, the code that uses mySQL seems not to be included into the jar file!

I added code for displaying messages in different parts of my code to find out at what point the problem is. It looks like the code that goes after line "Statement statement = connection.createStatement();" does not get included into the jar file.

Here is my main class code:


import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ResourceManagementApplication extends JFrame {
private static MainFrame mainFrame;

public static void main(String[] args) {
Database db = new Database();

try {
JOptionPane.showMessageDialog(null, "here 1",
"There was an error!", JOptionPane.ERROR_MESSAGE);

Connection connection = db.connect("jdbc:mysql://localhost:3306/", "root", "pw");

JOptionPane.showMessageDialog(null, "here 2",
"There was an error!", JOptionPane.ERROR_MESSAGE);

Statement statement = connection.createStatement();

JOptionPane.showMessageDialog(null, "here 3",
"There was an error!", JOptionPane.ERROR_MESSAGE);

statement.executeUpdate("USE ResourceManagementApplication;");
JOptionPane.showMessageDialog(null, "here 4",
"There was an error!", JOptionPane.ERROR_MESSAGE);

statement.close();
mainFrame = new MainFrame(connection);
}
catch( SQLException e ) {
e.printStackTrace();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}
If I create and run the jar file, I get error message saying "here 1" and "here 2", but nothing at all happens after that.

Any ideas why that could be? I really need to get this working!

Someone suggested that I need to include the JDBC driver into jar file. I have no idea how to do this - could someone help me with this (if this is, indeed, the solution)?
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

You probably just need to make sure that the jar file with the JDBC driver is in the classpath.
Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
[quote=Lucas Mireles]You probably just need to make sure that the jar file with the JDBC driver is in the classpath.[/quote]

Thanks for your reply. This sounds good. Do you mean just adding line 'Class-Path: mysql-connector-java-5.1.14-bin.jar' into manifest file? I just did that, it seemed to be working on my laptop, but won't work on another computer... am I missing something else?
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Basically I dont know how you are starting up your application or how your distributing the application across computers and without really knowing your situation its hard to give you a "best practice" recommendation.

Just off the top of my head i would imagine that you can probably simply unjar the driver jar and copy all the directories over to your jar file if you are distributing a single jar. If you have an IDE like Eclipse or IDEA then I believe you can export your application with the export wizard.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Perhaps you missed understanding the fact that the line you added to the manifest file means that the JDBC driver jar will be in the same directory as the executable jar, and failed to ensure that was the case on that other computer.
Olya Smith
Greenhorn

Joined: Dec 17, 2010
Posts: 12
Paul Clapham wrote:Perhaps you missed understanding the fact that the line you added to the manifest file means that the JDBC driver jar will be in the same directory as the executable jar, and failed to ensure that was the case on that other computer.


I did actually added it in and it sorted the original problem. The problem witht he second computer was that the driver was not installed in the program files/java folder.

Thanks anyone for help - it's working now!
Ferdiansyah Dolot
Greenhorn

Joined: Dec 20, 2010
Posts: 1
I use netbeans to include jar file in netbeans and it works
hope this might help
http://ferdidolot.wordpress.com/2009/06/14/java-mysql-jdbc-tutorial-using-netbeans-part-1/

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC not included into jar file?