• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!



 
Tamas Ragoncsa
Greenhorn
Posts: 6
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Terence Gronowski
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be useful reading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic