Java 为什么我无法获得 org.h2.Driver?我用 Maven
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32347944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I can't get the org.h2.Driver? I use maven
提问by joker
I face a problem about connecting to H2
我在连接 H2 时遇到问题
this is my pom.xml:
这是我的 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
总是显示 Class not found:org.h2.Driver
回答by Marko Jurisic
You should set scope to runtime so that h2 driver is packaged in your war file:
您应该将范围设置为运行时,以便将 h2 驱动程序打包到您的 war 文件中:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
<scope>runtime</scope>
</dependency>
回答by Vitalii
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.
我在 IntelliJ 上遇到了同样的问题,它找不到org.h2.Driver
. 我从网络上尝试了几种解决方案,但在简单重启 IntelliJ 后问题就解决了。
Hope this helps to save some time.
希望这有助于节省一些时间。
回答by mikelus
Found the answer here remove the runtime scope
在这里找到答案删除运行时范围
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
#removed this -> <scope>test</scope> #
</dependency>
回答by burtsevyg
My problem was in "" in docker-compose.yaml environment variables:
我的问题出在 docker-compose.yaml 环境变量中的“”中:
SPRING_DATASOURCE_DRIVER-CLASS-NAME="org.h2.Driver"
- it does not work
SPRING_DATASOURCE_DRIVER-CLASS-NAME=org.h2.Driver
- it work
SPRING_DATASOURCE_DRIVER-CLASS-NAME="org.h2.Driver"
- 它不起作用
SPRING_DATASOURCE_DRIVER-CLASS-NAME=org.h2.Driver
- 它起作用
回答by Sidharth K.Burnwal
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.
我已经阅读了所有可用的答案并尝试了所有答案。我不确定哪个有效,但是是的,它终于奏效了。因此,请尝试其中一种或全部,但它会解决您的问题。
- Remove the scope tag from your dependency first. It will look like
- 首先从您的依赖项中删除范围标记。它看起来像
<scope>test</scope>
or
<scope>runtime</scope>
<scope>test</scope>
或者
<scope>runtime</scope>
- There might be some issues with versioning.Mine working with this
- 版本控制可能存在一些问题。我正在处理这个问题
<version>1.4.190</version>
<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>
- Restart a few times might also help. I did for 2-3 times.
- 重新启动几次也可能有帮助。我做了2-3次。
Finally, my dependency looks like the below code.
最后,我的依赖看起来像下面的代码。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>