以下有关如何从资源文件夹初始化运行脚本的问题的已接受答案:INIT=RUNSCRIPT 和相对路径的问题 https://stackoverflow.com/questions/4490138/problem-with-init-runscript-and-relative-paths.
连接字符串:
jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'classpath:desktop/core/database/databaseCreation.sql'
但是我收到异常,即使文件存在,也找不到文件。
例外:
org.h2.jdbc.JdbcSQLNonTransientException: IO Exception: "java.io.FileNotFoundException: resource /desktop/core/database/databaseCreation.sql"; "classpath:desktop/core/database/databaseCreation.sql"; SQL statement:
RUNSCRIPT FROM 'classpath:desktop/core/database/databaseCreation.sql' [90031-210]
at [email protected] /cdn-cgi/l/email-protection/org.h2.message.DbException.getJdbcSQLException(DbException.java:573)
at [email protected] /cdn-cgi/l/email-protection/org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at [email protected] /cdn-cgi/l/email-protection/org.h2.message.DbException.get(DbException.java:216)
at [email protected] /cdn-cgi/l/email-protection/org.h2.message.DbException.convertIOException(DbException.java:461)
at [email protected] /cdn-cgi/l/email-protection/org.h2.command.dml.ScriptBase.openInput(ScriptBase.java:168)
at [email protected] /cdn-cgi/l/email-protection/org.h2.command.dml.RunScriptCommand.update(RunScriptCommand.java:52)
at [email protected] /cdn-cgi/l/email-protection/org.h2.command.CommandContainer.update(CommandContainer.java:174)
at [email protected] /cdn-cgi/l/email-protection/org.h2.command.Command.executeUpdate(Command.java:252)
at [email protected] /cdn-cgi/l/email-protection/org.h2.engine.Engine.openSession(Engine.java:279)
at [email protected] /cdn-cgi/l/email-protection/org.h2.engine.Engine.createSession(Engine.java:201)
at [email protected] /cdn-cgi/l/email-protection/org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:338)
at [email protected] /cdn-cgi/l/email-protection/org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:122)
at [email protected] /cdn-cgi/l/email-protection/org.h2.Driver.connect(Driver.java:59)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
at ispfdesktop/desktop.core.database.DatabaseManager.connectToInMemoryDatabase(DatabaseManager.java:184)
at ispfdesktop/desktop.core.database.CRUDTests.setUpBeforeClass(CRUDTests.java:21)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptBeforeAllMethod(TimeoutExtension.java:68)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllMethods$9(ClassBasedTestDescriptor.java:384)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllMethods(ClassBasedTestDescriptor.java:382)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:196)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:136)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.io.FileNotFoundException: resource /desktop/core/database/databaseCreation.sql
at [email protected] /cdn-cgi/l/email-protection/org.h2.store.fs.disk.FilePathDisk.newInputStream(FilePathDisk.java:386)
at [email protected] /cdn-cgi/l/email-protection/org.h2.store.fs.FileUtils.newInputStream(FileUtils.java:267)
at com.h2da[email protected] /cdn-cgi/l/email-protection/org.h2.command.dml.ScriptBase.openInput(ScriptBase.java:166)
... 69 more
尝试使用 getResource 访问文件(DatabaseManager 类位于desktop.core.database 包中)告诉文件存在:
String file = DatabaseManager.class.getResource("databaseCreation.sql").getFile();
boolean yes = new File(file).exists(); // yes is true
在 Windows(10) 和 Linux (Kubuntu) 平台上尝试并使用版本 2.1.210 的 h2。
更新#1
当文件位于根类路径文件夹中时找到文件classpath:databaseCreation.sql
,但在某些子文件夹(例如桌面/核心/数据库)下仍然不是。
在第一个子文件夹之前附加“/”不起作用。
尝试将 sql 文件放入所有文件夹。
'classpath:desktop/core/database/databaseCreation.sql'
不起作用
'classpath:desktop/core/databaseCreation.sql'
不起作用
'classpath:desktop/databaseCreation.sql'
不起作用
只有这个有效
'classpath:databaseCreation.sql'
不使用类路径:
如果不使用类路径,该文件可以位于任何子文件夹中,但这样,我需要使用文件的绝对路径:
jdbc:h2:mem:;INIT=RUNSCRIPT FROM '<absolute path here>'
让问题悬而未决,因为引用的问题已经过时了,这在当前的 H2 版本上不起作用,并且想知道如何使用它来工作classpath
.
更新#2
为了复制,您需要在项目中使用java模块化系统(module-info.java)。