Why I can't get the org.h2.Driver? I use maven
Asked Answered
P

6

21

I face a problem about connecting to H2

this is my pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>.</groupId>
    <artifactId>dbConnection</artifactId>
    <name>Db Connection</name>
    <packaging>war</packaging>
    <version>0.1</version>

    <dependencies>
        <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.176</version>
        </dependency>
    </dependencies>


</project>

and this is my main code

import java.sql.*;

public class DbConnection 
{
   static final String DB_URL = "jdbc:h2:tcp://localhost/~/test;AUTO_SERVER=TRUE";

   public static void main(String[] args) throws Exception
   {
        try
           { 
                Class.forName("org.h2.Driver");          
                Connection conn = DriverManager.getConnection(DB_URL,"sa","");  
                conn.close();
           }
       catch(ClassNotFoundException ex)
           {
                System.out.println( "ERROR: Class not found: " + ex.getMessage()); 
           }
    }
}

is always show up that Class not found:org.h2.Driver

Paronychia answered 2/9, 2015 at 8:15 Comment(1)
I assume that the exception occurs when running the jar generated by maven? Does you output jar contain the driver files?Roaster
A
36

You should set scope to runtime so that h2 driver is packaged in your war file:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.190</version>
    <scope>runtime</scope>
</dependency>
Assemblyman answered 10/12, 2015 at 13:49 Comment(1)
"A good example of dependencies that should use the runtime scope is a JDBC driver" from baeldung.com/maven-dependency-scopes . +1Bounded
S
28

I had the same problem with IntelliJ, it could not found org.h2.Driver. I tried several solutions from web but after simple restart of IntelliJ the problem was solved.

Hope this helps to save some time.

Sisterinlaw answered 7/3, 2018 at 14:13 Comment(2)
I'm using VS Code and sometimes this happens too, even running mvn install from the terminal works. Restarting the VS Code does solve the problem.Spirituous
This is what I needed and it worked after that.Cynewulf
F
7

Found the answer here remove the runtime scope

<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        #removed this -> <scope>test</scope> #
    </dependency>
Floccule answered 18/11, 2018 at 14:42 Comment(0)
G
3

I have read all the answers available and tried all of them. I am not sure which one worked but yes, It worked at last. So, try one of these or all but it will solve your problem.

  1. Remove the scope tag from your dependency first. It will look like
<scope>test</scope>

or

<scope>runtime</scope>
  1. There might be some issues with versioning.Mine working with this
<version>1.4.190</version>

you can also try these

<version>1.4.192</version> 
<version>1.4.195</version>
<version>1.4.197</version>
  1. Restart a few times might also help. I did for 2-3 times.

Finally, my dependency looks like the below code.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.190</version>
</dependency>
Galer answered 27/4, 2020 at 8:45 Comment(0)
P
1

If anyone doesn't want to use h2 aside from testing, keeping the <scope>test</scope>, just remove it for a moment, let IDE understand your line, and then put the scope back in. If that doesn't help try restarting IDE.

Worked in IntelliJ IDEA.

Psychosomatics answered 9/6, 2021 at 18:13 Comment(0)
W
0

My problem was in "" in docker-compose.yaml environment variables:

SPRING_DATASOURCE_DRIVER-CLASS-NAME="org.h2.Driver" - it does not work SPRING_DATASOURCE_DRIVER-CLASS-NAME=org.h2.Driver - it work

Warsle answered 3/3, 2020 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.