Accessing an H2.db file inside a jar?

328 views
Skip to first unread message

bee vo

unread,
May 30, 2012, 10:44:32 AM5/30/12
to H2 Database
Hey, is there a way to make a connection to a db file that is inside a
jar? I know that H2 has a feature that allows connection to a db
inside of zip file but how would I get a connection to a db file
inside a jar?

Ryan How

unread,
May 30, 2012, 11:04:08 AM5/30/12
to h2-da...@googlegroups.com
A jar is a zip file :)

Do you mean access it via classpath rather than naming the zip/jar file?

bee vo

unread,
May 30, 2012, 11:51:30 AM5/30/12
to H2 Database
Yes, I am trying to make a connection to a DB inside the same jar the
java classes which make the connection will be in.

Ryan How

unread,
May 30, 2012, 12:44:21 PM5/30/12
to h2-da...@googlegroups.com
I remember this coming up before a while ago (but I can't seem to find it).

I think you have to get the path of the jar file and then open it in h2
as you would a zip file, giving it the full path (or relative to the
current working directory).

So you have to use some classloader magic to get the jar file.

http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file

Then feed that into the h2 connection string

But there might be a better way :).

bee vo

unread,
May 30, 2012, 2:44:34 PM5/30/12
to H2 Database
If we were to assume that my data.h2.db is in my data.jar file which
is in my home directory:
So because a jar file is a zip, does that mean that:
ssConnection = new SSConnection("jdbc:h2:zip:~/
suppliers_and_parts.jar!/suppliers_and_parts", "sa", "");
should get me connected? Every time I've tried that it gives me a null
pointer exception because it is more than
likely looking for a .zip file. I've tried changing it to
"jdbc:h2:jar" and "jdbc:h2:file" but it gives me an exception
saying that it could not load properties.

Thanks for your help by the way.
On May 30, 11:44 am, Ryan How <r...@exemail.com.au> wrote:
> I remember this coming up before a while ago (but I can't seem to find it).
>
> I think you have to get the path of the jar file and then open it in h2
> as you would a zip file, giving it the full path (or relative to the
> current working directory).
>
> So you have to use some classloader magic to get the jar file.
>
> http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-ru...

Ryan How

unread,
May 30, 2012, 10:32:28 PM5/30/12
to h2-da...@googlegroups.com
I'm not sure on that sorry. What is the full exception? A NPE sounds a
bit odd, I would have thought you'd get something a bit more informative.

Thomas Mueller

unread,
Jun 4, 2012, 4:43:31 AM6/4/12
to h2-da...@googlegroups.com
Hi,

See the documentation at Features / Read Only Databases in Zip or Jar File.

Regards,
Thomas
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Reply all
Reply to author
Forward
0 new messages