wood burning stoves 2.0*
The moose likes JDBC and the fly likes Jdbc driver not found when putting an application in a jar-file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Jdbc driver not found when putting an application in a jar-file" Watch "Jdbc driver not found when putting an application in a jar-file" New topic
Author

Jdbc driver not found when putting an application in a jar-file

Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
I want to pack my application in a .jar file.

I run an application which needs sqljdbc4.jar to access a Sql 2008 Express server. Without the jar the application runs well and gets access to the database. With the jar file it doesn't.

I wrap as follows:

Make Manifest.txt with the following contents: Main-Class: MyMainClass (plus empty line)

Make executable jar: jar -cfm MyAppl.jar Manifest.txt *.class

Run: java -jar MyAppl.jar

Now the database is not found => Exception: com.microsoft.sqlserver.jdbc.SQLServerDriver

Copiing sqljdbc4.jar to jre/lib/ext is useless.

The same problem exists with the MySql driver.

Help needed!




Docendo discimus (we learn by teaching)
SCJP 5 Zertifizierung, Vorbereitungsbuch; SCJP 6 Zertifizierung, Vorbereitungsbuch

Tamas Ragoncsa
Greenhorn

Joined: Feb 25, 2011
Posts: 6

Terence,

it seems your sqljdbc4.jar is not accessible when you run your application. Since you can't specify additional jars to the class path when using -jar, I would suggest to go with

java -cp MyAppl.jar;sqljdbc4.jar MyMainClass

Tamas
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Thanks for the tip, Tamas

I just want one file to cklick on. When I have the application in a .class file, it works well (I have sqljdbc4.jar in the classpath). I do not understand, if I have the folder wrapped in a .jar file, sqljdbc4.jar is not found any more.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41635
    
  55
If you want to use additional jar files, then you need a Class-Path attribute in the jar file manifest: http://download.oracle.com/javase/tutorial/deployment/jar/downman.html

If you really want just a single jar file, then you need to package the classes that make up the JDBC driver along with your application classes (adding the driver jar file as it is to your app's jar file will not work).


Ping & DNS - my free Android networking tools app
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Thanks Ulf

This is the corect answer.

To summarize:

Creating a Manifest.txt file with the following contents in the program folder:

Manifest-Version: 1.0
Class-Path: sqljdbc4.jar
Main-Class: ParkplatzVerwaltung
(Newline)

Don't forget to end with a newline. The important thing is "Class-Path: sqljdbc4.jar", showing where the driver is.

I copied sqljdbc4.jar in the same folder as the program.

Creating a jar of the program folder:

jar cfm pp.jar Manifest.txt *.*

With *.* I include alle files, the sqljdbc4.jar, too. When I delete sqljdbc4.jar the pp.jar works well nevertehless, I suppose that sqljdbc4.jar is found in pp.jar.





Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


When I delete sqljdbc4.jar the pp.jar works well nevertehless, I suppose that sqljdbc4.jar is found in pp.jar.

Jar files don't work that way - you can't have one containing a dependent jar. Are you sure it's not on the classpath elsewhere?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Uups

I still have it in the Classpath elsewhere. But I thought it would use the sqljdbc4.jar in the pp.jar folder. If this is not the case I do not understand that one must have

Class-Path: sqljdbc4.jar

in the Manifest.txt file.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41635
    
  55
I still have it in the Classpath elsewhere.

What makes you say that? What *is* the classpath - do you have a CLASSPATH environment variable? If so, I advise to remove it and never to use it again; it causes more problems than it solves.

But I thought it would use the sqljdbc4.jar in the pp.jar folder.

It will. If you double-click a jar file then the classpath consists of the jar file and whatever else is listed in its manifest.
Terence Gronowski
Ranch Hand

Joined: Dec 19, 2007
Posts: 64
Ulf, many thanks

sqljdbc4.jar is found in pp.jar indeed. I deleted the classpath I defined in the system and database access is granted nervertheless.

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This might be useful reading.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Jdbc driver not found when putting an application in a jar-file