Ash*_*ins 29 java jdbc h2 classnotfoundexception
我正在尝试使用H2连接到Java中的数据库(使用Eclipse作为IDE).样本确实(下面)抛出一个ClassNotFoundException.问题是,我确实将h2 jar文件添加到系统CLASSPATH中.我甚printenv至在控制台中检查了几次.我省略了一步吗?
码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
rog*_*ack 31
在我的情况下(有点无关,但值得一提),我将此添加到我的maven pom中,错误消息消失了:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>xxx</version> <!-- ex: 1.2.140 -->
</dependency>
Run Code Online (Sandbox Code Playgroud)
或者如果您在单元测试期间仅使用h2:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>xxx</version> <!-- ex: 1.2.140 -->
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
naX*_*aXa 14
最近我在使用最新版本(1.4.196)的H2驱动程序时遇到了java.lang.ClassNotFoundException: org.h2.DriverIntelliJ IDEA 2017.2 EAP的异常.解决方案是降级到1.4.195工作.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.195</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
示例(下面)抛出ClassNotFoundException
然后驱动程序不在类路径上.
问题是,我确实将h2 jar文件添加到系统CLASSPATH中.我甚至在控制台中通过'printenv'检查了几次.
你是怎么做到的?请显示获得的输出.
我省略了一步吗?
我不能用提供的信息说.但是依赖CLASSPATH环境变量无论如何都是一种不好的做法,-cp如果你在命令行上运行Java ,你应该使用该选项.像这样:
java -cp h2.jar com.acme.Program
Run Code Online (Sandbox Code Playgroud)
有没有办法在我使用RUN菜单时设置Eclipse使用jar文件,这样我就不必一直从控制台运行?
是.在Eclipse下,将JAR添加到项目构建路径:右键单击项目,然后单击Properties> Java Build Path> Libaries> Add JARS ...(假设H2 JAR在相对于项目的目录中可用).其他IDE有相同的方法来做到这一点.
我遇到以下错误(使用Intellij)
org.h2.Driver的java ClassNotFoundException
通过从我的pom中删除范围来解决它。
原为:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
变成:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我们将Maven Quickstart项目实现为对另一个项目的依赖项时,将出现此类错误。通常仅作为对junit的测试而发生。因此,在应用程序中它将不起作用。
| 归档时间: |
|
| 查看次数: |
63550 次 |
| 最近记录: |