You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Daniel Morton <dj...@yahoo.com> on 2006/04/02 19:59:26 UTC

Storing/Accessing a Derby database inside a JAR

Hello all:

I have created a derby database called NameDB which
contains a table which contains a list of 10000 names.
 I also have a class called djm.NameGenerator which
when constructed, connects to the database.  Methods
in the class then get names from the database.  It
works fine seperately, but I wish to put the database,
and the class (and eventually the derby.jar if
possible) into a jar file that is easily distributable
so the whole thing is self contained.

I created a jar using:

jar cf NameGenerator.jar djm NameDB

so that the database, and the NameGenerator class in
the djm package is put in to the jar.  However, if I
run a test.java program to create a NameGenerator
object, it errors when trying to connect to the
database:  SQLException: Database '/NameDB' not found.

My connection string (per the Derby Documentation) is:
DriverManager.getConnection("jdbc:derby:/NameDB");
I've tried multiple varitions on this (ie. NameDB,
/NameDB, /NameDB/ etc.)

And in the above, the NameGenerator.jar is in the
classpath.

I've also tried specicifying the explicit path (ala
the Derby Manual):
DriverManager.getConnection("jdbc:derby:jar:(C:/project/NameList/NameGenerator.jar)/NameDB");

Which results in the SQLException: Failed to start
database
'jar:(C:/project/NameList/NameGenerator.jar)/NameDB'
Java Exception: The system cannot find the file
specified: java.util.zip.ZipException.

I'm really at a loss here.  Can anyone tell me what
I'm doing wrong?  I've read through all the relevant
sections of the Derby documentation, and I'm not
really sure what to do next.  Also, once I get this
sorted out, is there a way to include derby.jar inside
NameGenerator.jar so that just the one file could be
distributed?

Any help that anyone can offer would be greatly
appreciated.

Daniel Morton

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Storing/Accessing a Derby database inside a JAR

Posted by Susan Cline <ho...@pacbell.net>.
Hi Daniel,

I have not tried this myself, but looking at the documentation 
and looking at the error I wonder if the the path to the database 
from within the jar file is correct.

Please see my specific comments below.

Susan

--- Daniel Morton <dj...@yahoo.com> wrote:

[ snip ]
> 
> I created a jar using:
> 
> jar cf NameGenerator.jar djm NameDB
> 
> so that the database, and the NameGenerator class in
> the djm package is put in to the jar.  However, if I
> run a test.java program to create a NameGenerator
> object, it errors when trying to connect to the
> database:  SQLException: Database '/NameDB' not found.
> 
> My connection string (per the Derby Documentation) is:
> DriverManager.getConnection("jdbc:derby:/NameDB");
> I've tried multiple varitions on this (ie. NameDB,
> /NameDB, /NameDB/ etc.)
> 
> And in the above, the NameGenerator.jar is in the
> classpath.

I believe trying to access the database this way will not work
as per the documentation, you need to use the jdbc:derby:jar
syntax as you have below.

> 
> I've also tried specicifying the explicit path (ala
> the Derby Manual):
> DriverManager.getConnection("jdbc:derby:jar:(C:/project/NameList/NameGenerator.jar)/NameDB");
> 
> Which results in the SQLException: Failed to start
> database
> 'jar:(C:/project/NameList/NameGenerator.jar)/NameDB'
> Java Exception: The system cannot find the file
> specified: java.util.zip.ZipException.

This error looks more hopeful :-)  At least it is looking
for the /NameDB in the jar file.  But I'm thinking that
the path to the NameDB relative to the jar file is the
problem.  When you "unjar" the NameGenerator.jar file
what is the path to NameDB?  What happens if you try
this URL instead;

'jar:(C:/project/NameList/NameGenerator.jar)NameDB'
> 
[ snip ]
> Also, once I get this
> sorted out, is there a way to include derby.jar inside
> NameGenerator.jar so that just the one file could be
> distributed?
> 
I don't know the answer to this, but it seems like it might
be tough since it would be a chicken-and-egg situation.  In 
order to use the Derby JDBC connection URL it would need to
have derby.jar in the classpath first.  Hopefully someone else
can help you with this one.