java ClassNotFoundException for org.h2.Driver

前端 未结 13 2498
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 10:40

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

13条回答
  •  情歌与酒
    2021-01-01 11:12

    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.

提交回复
热议问题