It's not a secret anymore!*
The moose likes JDBC and the fly likes Problem with getConnection() after exporting project to a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with getConnection() after exporting project to a jar file" Watch "Problem with getConnection() after exporting project to a jar file" New topic
Author

Problem with getConnection() after exporting project to a jar file

Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Hi!

My problem is this: I wrote a small test program that works with jdbc, using Microsoft SQL Server 2000 and Eclipse. The program is very similar to the test code on this page: http://support.microsoft.com/kb/313100 and everything works fine in Eclipse but after exporting the project to a jar file it won't run properly. The problem seems to be with the connection in this part of the code:



To establish a connection with the database I added these external archives to my project: mssqlserver.jar, msutil.jar and msbase.jar.

Thanks for your help!

Cheers

[ September 25, 2007: Message edited by: Ivan Bosnjak ]
[ September 25, 2007: Message edited by: Ivan Bosnjak ]
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
What's the exception that you get? Could you post the stack trace?
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Originally posted by Tarun Yadav:
What's the exception that you get? Could you post the stack trace?


After running the jar file I get the message: com.microsoft.jdbc.sqlserver.SQLerverDriver

I added the claspath variable and it still doesn't work, I also registered the driver in a way explained on the mentioned link...



I still don't understnd what's wrong
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


After running the jar file I get the message: com.microsoft.jdbc.sqlserver.SQLerverDriver

There will be more to that message. What else does it contain? Could you post the stack trace?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Originally posted by Paul Sturrock:

There will be more to that message. What else does it contain? Could you post the stack trace?


Ok, just to be sure you understand what I'm saying I'll repeat myself: the program works like a charm in Eclipse and no error are displayed in the console, but nothing happened when I exported and ran the jar file so I added the part of the code where the program catches the exception and displays an error message:



This option pane shows this message: java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver

I don't know how to export the stack trace to a file to give you a full description of the error...
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
This is the full error message, I manged to export it to a file.

Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
You should be able to see the stack trace in your console window if you run the JAR from the command line instead.

Also, you could try exploding the JAR and checking if your drivers are included in it or not.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver

So you don't have the Microsoft JDBC driver in your application's classpath. Make sure you have the driver jar files in your classpath and it should work fine.
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Originally posted by Paul Sturrock:

So you don't have the Microsoft JDBC driver in your application's classpath. Make sure you have the driver jar files in your classpath and it should work fine.


I set the classpath (Win XP) in Start/Control Panel/System/Advanced/Environment Variables/ as system variables and user variables in this way:

.;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar

Still doesn't work...
Ivan Bosnjak
Greenhorn

Joined: Jun 08, 2007
Posts: 15
Originally posted by Ivan Bosnjak:

Still doesn't work...[/QB]


...and it works now

I copied the three files (msbase.jar, mssqlserver.jar and msutil.jar) in a folder C:\Program Files\Java\jre1.6.0_02\lib\ext

Does that mean my program won't work if I install a new version of jre?

Thanks for the help
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I copied the three files (msbase.jar, mssqlserver.jar and msutil.jar) in a folder C:\Program Files\Java\jre1.6.0_02\lib\ext

Well, that is a bit of a "brute force" approach to fixing classpath issues. The problem remains that your application's classpath is not right, however you have side-stepped this problem by including the driver in your JRE. So it does mean if you change you JRE your applciation is broken again. Have a read of this - learning about the classpath and how to set it will make your life easier in the future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with getConnection() after exporting project to a jar file