5

I use H2 database for a desktop application in embedded mode. When I compress the application into jar file the database file is omitted. So, when I run MyApplication.jar nothing works. What is the correct way to embed/include/connect h2.jar file with MyApplication.jar? Or maybe there is another way to deliver database and application in the bundle?

Clifford
  • 88,407
  • 13
  • 85
  • 165
bancer
  • 7,475
  • 7
  • 39
  • 58
  • How are you creating this JARfile? Are you using a build tool such as Maven, or simply running `jar` from the command-line? – Anon Dec 14 '10 at 20:39
  • I use Eclipse. There is an option to export to jar file, so I use that option. – bancer Dec 14 '10 at 22:54
  • I recommend taking the time to learn Maven: http://www.sonatype.com/books/mvnref-book/reference/public-book.html -- over the long term, that investment will repay itself many times over. – Anon Dec 15 '10 at 15:23

4 Answers4

3

One common scheme is to put h2.jar in a lib directory relative to your application and include a Class-Path entry in your JAR's manifest with an entry for it:

Class-Path: lib/h2.jar lib/…

Addendum: This small project includes a link to the JAR specification and a handy utility for examining the manifest in situ.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
3

If you are using Maven to build your project, just use maven-shade-plugin... great results and control, I've been using it a lot.

For embedding all your dependencies you would write something like:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Then with just a normal "mvn package" you'll have all your dependencies' jars included in your application jar.

With further configuration you can control what gets included and what not.

Pablo Lalloni
  • 2,615
  • 19
  • 20
2

If you want to put the database itself in your jar file, then this explanation might help: http://www.h2database.com/html/features.html#database_in_zip

This is also discussed in this thread in the H2 forum.

0

If you're using maven to build you project take a look maven-assembly-plugin (jar-with-dependencies). This would produce single jar with all dependencies packed into it.

maximdim
  • 8,041
  • 3
  • 33
  • 48