43

I am trying to use H2 to connect to a database in Java (using Eclipse as the IDE). The sample does (below) throws a ClassNotFoundException. The thing is, I did add the h2 jar file to the system CLASSPATH. I have even checked it's there several times via printenv in the console. Am I omitting a step?

CODE:

import java.sql.*;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) 
  throws Exception{

  try{
   System.out.println("hello, world!");
   Class.forName("org.h2.Driver");
   Connection conn = DriverManager.getConnection("jdbc:h2:~/testdb", "sa", "");
   // add application code here
   conn.close();
  }catch(ClassNotFoundException ex){
   System.out.println( "ERROR: Class not found: " + ex.getMessage() );

  }
  System.exit(0);

 }

}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Ashton Wilkins
  • 431
  • 1
  • 4
  • 3
  • How did you add the Jar to your classpath? To be sure, I would create a folder in your project called 'lib', copy the jar into it, then right click and select 'Build path > Add to build path'. – William Oct 24 '10 at 11:48
  • Could you please format your code properly - select the code and use the 5th button (Code Sample) on the editing toolbar while in editing mode. – Neeme Praks Oct 24 '10 at 12:18
  • How are you running your code? Inside Eclipse or from command line? How did you add h2.jar in the classpath? How did you check for the existence of h2.jar in the classpath? – Neeme Praks Oct 24 '10 at 12:24
  • I added it to the CLASSPATH using the console: sudo gedit /etc/environment, then manually added a line of the form key="value". I then logged out and logged in, checked, and it's there. – Ashton Wilkins Oct 24 '10 at 13:36
  • Neeme - I am running the code from inside Eclipse, using the RUN menu. – Ashton Wilkins Oct 24 '10 at 13:36
  • William - thanks, I am new to java development (I confess to being a C# guy, but java is growing on me). Your recommendation worked: i.e., I added the jar to the build path in Eclipse, did a rebuild, and the ClassNotFoundException was not longer thrown. Thanks again! – Ashton Wilkins Oct 24 '10 at 13:46

16 Answers16

41

In my case (unrelated a bit, but worth mentioning), I added this to my maven pom, and the error message went away:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>xxx</version> <!-- ex: 1.2.140 -->
  </dependency>

or if you are only using h2 during unit testing:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>xxx</version> <!-- ex: 1.2.140 -->
    <scope>test</scope>
  </dependency>
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 1
    Might want to add `test` since that's how it's probably most often used. Latest version is available here: http://www.h2database.com/html/main.html – GlenPeterson Nov 14 '16 at 16:32
21

I was having the following error (using Intellij)

java ClassNotFoundException for org.h2.Driver

Solved it by removing the scope from my pom.

was:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>test</scope>
    </dependency>

changed to:

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
    </dependency>

This type of error will come when we are implementing Maven Quickstart project as a dependency to another project. Mostly occurs as test only for junit. So in application it will not work.

SRINIVAS
  • 3
  • 1
Ali_Nass
  • 838
  • 1
  • 9
  • 27
16

Recently I encountered the java.lang.ClassNotFoundException: org.h2.Driver exception in IntelliJ IDEA 2017.2 EAP while using the latest version (1.4.196) of H2 driver. The solution was to downgrade to 1.4.195 that worked.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.195</version>
    <scope>test</scope>
</dependency>
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
8

The sample does (below) throws a ClassNotFoundException

Then the driver is not on the classpath.

The thing is, I did add the h2 jar file to the system CLASSPATH. I have even checked it's there several times via 'printenv' in the console.

How did you do that exactly? Please show the obtained output.

Am I omitting a step?

I can't say with the provided informations. But relying on the CLASSPATH environment variable is a bad practice anyway and you should use the -cp option if you're running Java on the command line. Like this:

java -cp h2.jar com.acme.Program

Is there a way I can set Eclipse to use the jar file when I use the RUN menu so that I don't have to run from the Console all the time?

Yes. Under Eclipse, add the JAR to the project build path: right-click on your project then Properties > Java Build Path > Libaries > Add JARS... (assuming the H2 JAR is available in a directory relative to your project). Others IDE have equivalent way of doing this.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Thanks. I did not know CLASSPATH was poor policy. Is there a way I can set Eclipse to use the jar file when I use the RUN menu so that I don't have to run from the Console all the time? – Ashton Wilkins Oct 24 '10 at 13:37
4

In my case(I use sbt) change

libraryDependencies += "com.h2database" % "h2" % "1.4.196" % Test

to

libraryDependencies += "com.h2database" % "h2" % "1.4.196"
Xianhong Xu
  • 107
  • 1
  • 4
3

If you use Gradle change dependency in build.gradle:

testCompile group: 'com.h2database', name: 'h2', version: '1.4.199'

to

compile group: 'com.h2database', name: 'h2', version: '1.4.199'
Sanchez
  • 51
  • 5
2

The <scope>[database_name]</scope> should include the database you are working with. If you change your db from one to another, make sure to change the scope also. As soon as i changed it the error went away.

1

Use release version.

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
1

In my case it's actually the connection string issue. I saw this.

After I added the mem in the URL string below, and it worked.

String url = "jdbc:h2:mem:~/test";
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
uudaddy
  • 341
  • 4
  • 9
1

Remove the scope tag

<scope>test</scope>
KR93
  • 1,098
  • 11
  • 10
0

Using <scope>test</scope> should not work logically. try it with <scope>runtime</scope> or <scope>provided</scope>, unless you need it only for testing phase.

On maven docs, it says that <scope>test</scope> dependency is not required for normal use of the application, and is only available for the test compilation and execution phases
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Aman
  • 1,627
  • 13
  • 19
0

I have faced same issue when using : 2 times in h2 database, because Driver manager can not identify proper connection type

  • when you use "jdbc:h2:localhost:123/db", then it divide into "jdbc","h2:localhost","123/db",
  • so here expected value is h2 but getting h2:localhost because of issue in core regex for identify driver class from list of loaded drivers

Use full path like:

  • Don't: "jdbc:h2:testdb",         Do:"jdbc:h2:/c:/.../testdb"
  • Don't: "jdbc:h2:localhost:123/db",    Do: "jdbc:h2://localhost:123/db"
0

I am working with intelliJ. I had the "h2-1.4.200" in the lib folder. I tried every suggestion, ranging from , to . Strangely, my problem got solved only by going to these places : Project Structure -> Artifact -> Output Layout -> Available Elements and then expanding the content of the folder and then right click on "h2-1.4.200" and finally select "Extract Into Output Root". :) The strange solution

0

This worked with testRuntimeOnly 'com.h2database:h2:1.4.200'

and in application.yaml property remove or comment out # driverClassName: org:h2:Driver

As per springboot latest documentation jdbc driver is not required as it is automatically detected.

Sheel Prabhakar
  • 409
  • 2
  • 6
0

Ran into this issue and steps to fix:

1 - Corrected this org.h2.driver to org.h2.Driver

2 - Verify that the scope of h2 dependency in pom.xml is not set to test, if that is the case, remove it so your dependency is similar to:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
</dependency>

3 - Finally, I had to run mvn clean verify compile. This will refresh the classes in target folder and add the h2 class to your classpath if it is not there.

Hope it helps :)

Koji D'infinte
  • 1,309
  • 12
  • 20
-1

I use sbt. In build.sbt, I removed the "h2" dependency and included this instead: "com.h2.database" % "h2" % "1.4.200" And it worked!

Divyanshi Mangal
  • 73
  • 1
  • 3
  • 11
  • this seems like a copy of https://stackoverflow.com/a/48676896/1392312 to me – Rossiar Apr 22 '20 at 11:43
  • No, that person has just removed "test" from dependency. I just had "h2" dependency added, and I have added a couple of changes to it. Please read both the answers carefully. – Divyanshi Mangal Apr 29 '20 at 15:15