【问题标题】:Initialize H2 in-memory from a file contained in a jar?从 jar 中包含的文件初始化内存中的 H2?
【发布时间】:2013-04-01 10:38:53
【问题描述】:

如何initialize H2 with a file 包含在一个罐子里?

目前我遇到了这个异常:

testCase(CheckTest): org.h2.jdbc.JdbcSQLException: IO 异常: “java.io.FileNotFoundException: /Users/user1/.m2/repository/a/3.10-SNAPSHOT/a-SNAPSHOT-tests.jar!/h2_db.sql (没有相应的文件和目录)”; “文件:/Users/user1/.m2/repository/a/3.10-SNAPSHOT/a-3.10-SNAPSHOT-tests.jar!/h2_db.sql”; SQL 语句:(..)

我要初始化数据库的文件(h2_db.sql)在jar文件a-3.10-SNAPSHOT-tests.jar

代码看起来像

initSqlFile = getInitSqlFile();
//initSqlFile = "/Users/user1/.m2/repository/a/3.10-SNAPSHOT/a-SNAPSHOT-tests.jar!/h2_db.sql"
//initSqlFile is a file contained ina jar, note the "!"

url = String.format(
      "jdbc:h2:mem:%s;MODE=PostgreSQL;LOCK_TIMEOUT=500;ifexists=true;INIT=runscript from '%s';DB_CLOSE_DELAY=-1;MVCC=TRUE",
       testSuitName, initSqlFile);

// later ...
con = DriverManager.getConnection(url, user, passwd);

虽然这个问题可以相对容易地解决,但我想知道 H2 是否支持从 jar 文件中的文件进行初始化。

或者也许是一个优雅的解决方法,它并不意味着临时文件。



其他信息: 有一个查找资源的方法,如果类路径中的resource is found 该方法返回resource's path。然后资源的路径(/Users/user1/.m2/repository/a/3.10-SNAPSHOT/a-SNAPSHOT-tests.jar!/h2_db.sql)作为参数传递给initializes the database的方法。

【问题讨论】:

  • @ThomasMueller 刚刚编辑了问题。顺便说一句,H2 很棒!
  • “你尝试了什么”我的意思是:你能提供你用过的源代码吗?
  • @ThomasMueller 添加了一些代码

标签: java h2


【解决方案1】:

As documented, the file name must start with the zip: prefix:

zip:/Users/user1/.m2/repository/a/3.10-SNAPSHOT/a-SNAPSHOT-tests.jar!/h2_db.sql

【讨论】:

猜你喜欢
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多