1

So I followed the tutorial on the H2 Documentation page and used the "Connecting to a Database using JDBC" method of connecting to the database. I First added the h2-*.jar file to the Lib Folder (through Netbeans) and used the following to make the connection to my database I previously created.

 Class.forName("org.h2.Driver");
 connection = DriverManager.getConnection("jdbc:h2:~/" + DatabaseName);

This turned out to work in the IDE environment, however when I attempted to run the application directly from the jar executable I get the following error:

 java.lang.ClassNotFoundException: org.h2.Driver ...

this error occurs at the Class.forName() class loader. So I did a little looking around and found that this was a prominent problem. One solution people use was to extract the class Loader from the current Thread as so:

 Thread t = Thread.currentThread();
 ClassLoader cl = t.getContextClassLoader();
 cl.getClass().getClassLoader();
 Class toRun = cl.loadClass("org.h2.Driver");

Unfortunately this seems to still result in the same error, so i'm wondering what i'm doing wrong. Should I be doing something to make sure the Driver is in the class path? I have no I idea how if that's the case.

Thanks!

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
Sammy Guergachi
  • 1,986
  • 4
  • 26
  • 52
  • "One solution people use was to extract the class Loader from the current Thread" - where did you find that? It not related to this problem. – Thomas Mueller Feb 26 '12 at 07:43

1 Answers1

0

You need to add the h2-*.jar file to the classpath when running the application, for example using

java -cp h2*.jar -jar yourApp.jar
Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
  • should't adding it to the Libraries folder through the IDE do that? I use other libraries and all I had to do was add to the libraries and I can freely refer to the specific package using class.getResources("package.name") – Sammy Guergachi Feb 27 '12 at 06:25
  • Sorry, I don't know what 'adding to the Libraries folder of the IDE' means. – Thomas Mueller Feb 27 '12 at 10:18
  • You wrote "when I attempted to run the application directly from the jar executable". That means outside Netbeans, right? If yes, then the Netbeans config might not help at all. – Thomas Mueller Feb 27 '12 at 10:22