从包含在jar中的文件初始化H2内存?

问题描述:

如何将initialize H2 with a file包含在jar中?从包含在jar中的文件初始化H2内存?

目前我得到这个异常:

测试用例(CheckTest):org.h2.jdbc.JdbcSQLException:IO异常: “java.io.FileNotFoundException: /Users/user1/.m2/ repository/a/3.10-SNAPSHOT/a-SNAPSHOT-tests.jar!/h2_db.sql (没有这样的文件或目录)“; “file:/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文件中的文件初始化。

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



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

+0

那你试试? –

+0

@ThomasMueller刚刚编辑了这个问题。顺便说一下,H2很棒! – user454322

+0

“你尝试了什么”我的意思是:你能提供你使用过的源代码吗? –

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 
+0

谢谢。 我已经找到答案,而浏览这个其他问题http://*.com/questions/12069224/how-to-compress-memory-space-of-h2-database-in-memory-mode/12071445 – user454322