设置 H2 init 运行脚本时出现问题。使用 java 模块化系统时,当脚本不在类路径根文件夹中时找不到文件

2024-03-25

以下有关如何从资源文件夹初始化运行脚本的问题的已接受答案: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)。


UPDATE:(模块信息.java)

根据您的评论,您的原始设置使用了 JDK 9+ 模块。这是一个复杂且令人困惑的话题。最简单的方法是简单地删除 module-info.java 并且不使用模块。如果您打算使用模块并将资源保存在单独的目录(模块)中,则有多种选择,但没有一个明确的选择。也许最简单的选择是打开包含资源的“包”。在我的本地测试中,这样的事情有效:

module myAppModule {
    exports desktop.core.database;

    opens desktop.core.database;

    requires java.sql;
    requires org.junit.jupiter.api;
}

更多信息:

  • https://www.oracle.com/corporate/features/understanding-java-9-modules.html https://www.oracle.com/corporate/features/understanding-java-9-modules.html
  • 在 Java 9 中加载类和资源 https://stackoverflow.com/questions/45166757/loading-classes-and-resources-in-java-9
  • 访问 java jigsaw 模块中的资源文件 https://stackoverflow.com/questions/53285898/accessing-resource-files-in-a-java-jigsaw-module
  • 如何使用 Java 9 中的类加载器访问资源 https://stackoverflow.com/questions/48768879/how-to-access-resource-using-class-loader-in-java-9
  • 从外部模块访问资源文件 https://stackoverflow.com/questions/46861589/accessing-resource-files-from-external-modules

ORIGINAL

我无法使用非常简单的 Maven 设置来复制您的问题,仅使用 H2 运行时作为依赖项(相同版本,2.1.210)以及 JUnit 5。它按预期工作。

堆栈跟踪显示这是在 Eclipse 中作为测试运行的。如果我手动添加,我可以在 Eclipse 中准确复制您的问题src/main/resources作为 Java 构建路径源文件夹,but排除**/这将排除资源的子目录。

如果您使用的是 Maven,也许可以尝试在命令行上运行mvn test验证它不是您的 Eclipse 设置。

否则,您将需要发布更多信息才能获得帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设置 H2 init 运行脚本时出现问题。使用 java 模块化系统时,当脚本不在类路径根文件夹中时找不到文件 的相关文章

  • 在 Java Swing 中检测 JScrollPane 上的 mouseClick 事件

    如果我有这样的东西 我可以使用布尔标志 performAdjustment 控制自动滚动 static boolean performAdjustment true JTextArea textArea new JTextArea JScr
  • 从 OMElement 对象获取 InputStream/io.Reader

    我有一个OMElement对象 从中我想得到一个InputStream或读者对象 我想要的是流式传输xml来自OMElement我有 没有加载到内存中 我只能得到XMLStreamReader对此表示反对 但我找不到办法得到InputStr
  • Java Swing:清除JList而不触发监听器

    我的情况如下 我有一个 JList 只要在列表中进行选择 它就会触发搜索 使用 ListSelectionListener 我正在尝试使用以下命令重置列表上的选择list clearSelection 这样做的问题是使用clearSelec
  • Spring webflow 应用程序:HTTP 302 暂时移动

    我的 java 应用程序中的每个请求都会生成另外 2 个带有 HTTP 302 错误的请求 例如 如果请求查看名为板 html 这个请求是从首页 html 我收到按以下顺序生成的 3 个请求 POST home html 302 Moved
  • 如何解决错误:java.lang.ClassNotFoundException:io.netty.util.concurrent.GenericFutureListener?

    昨天我第一次尝试用 Java 制作 Prometheus 客户端 从 Python 开始 最后是 GoLang 是否找到示例 import io prometheus client Counter import io prometheus
  • 在气球内显示带有照片的多个地标的最佳做法是什么?

    我有一个项目如下 从手机上拍摄几张照片 将照片保存在网络系统中 然后将照片显示在其中的谷歌地球上 我读过很多文章 但它们都使用 fetchKml 我读过的一篇好文章是使用 php 但使用 fetchKml 我不知道是否可以使用 parseK
  • 如何在 OpenAPI 3.0 中定义字节数组

    我正在将 API 从 Swagger 2 0 迁移到 OpenAPI 3 0 在 DTO 中 我有一个指定为字节数组的字段 Swagger 对 DTO 的定义 Job type object properties body type str
  • Java:检查给定日期是否在当前月份内

    我需要检查给定的日期是否在当前月份 我编写了以下代码 但 IDE 提醒我getMonth https docs oracle com javase 7 docs api java util Date html getMonth and ge
  • 如何消除警告:使用“$”而不是“.”对于 Eclipse 中的内部类

    我是 Android 开发新手 当我将 eclipse 和 Android SDK 更新到最新版本后 我收到警告 Use instead of for inner classes or use only lowercase letters
  • Java 的 QP 求解器 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • LibGdx 如何使用 OrthographicCamera 滚动?

    我已经找了 10 个小时 字面意思 我已经完成了 我需要问一下 事情是我正在学习如何使用 LibGdx 来编写 Java 游戏 我正在做一个水平太空飞船游戏 所以 我最糟糕的问题是我不知道如何滚动 我认为绘制会更好地解释 我想绘制一个巨大的
  • 如何自定义JProgressBar?

    我正在制作一个启动器 我想要一个自定义的进度栏 我已经做了一些研究 并且可以使用 JavaFX 从未用它做过任何事情 并且可以通过替换 UI 来实现 我正在寻找一个具有圆形边缘和圆形填充的酒吧 像这样的事情 package gui impo
  • java JFileChooser 文件大小过滤器

    我知道我可以按文件类型进行过滤 但是可以按文件大小进行过滤吗 例如 JFileChooser 仅显示 3 MB 以内的图片 简短的回答应该是 你尝试过什么 长答案是肯定的 JFileChooser fc new JFileChooser f
  • Java G1 GC 处理引用对象运行缓慢

    我已经在 J ava 上运行了计数器 它24小时工作 每秒点击通过100次左右 白天 GC 处理时间从 20 60 毫秒缓慢上升到 10000 60000 毫秒 然后下降到 20 60 毫秒 这种模式不时地重复 从 GC 日志中我发现 GC
  • RMI 服务器:rmiregistry 或 LocateRegistry.createRegistry

    对于服务器端的RMI 我们需要启动吗rmiregistry程序 或者只是调用LocateRegistry createRegistry 如果两者都可以的话 各有什么优点和缺点 他们是同一件事 rmiregistry是一个单独的程序 您可以从
  • 防止 Firebase 中的待处理写入事务不起作用

    我的目标是在单击按钮时将名称插入 Cloud Firestore 中 但如果用户未连接到互联网 我不希望保存处于挂起状态 我不喜欢 Firebase 保存待处理写入的行为 即使互联网连接已恢复 我研究发现Firebase 开发人员建议使用事
  • Java的hashCode可以为不同的字符串产生相同的值吗?

    使用java的哈希码函数是否可以为不同的字符串提供相同的哈希码 或者如果可能的话 其可能性的 是多少 Java 哈希码是 32 位 它散列的可能字符串的数量是无限的 所以是的 会发生冲突 百分比是没有意义的 项目 字符串 的数量是无限的 而
  • Java泛型类型

    当我有一个界面时 public interface Foo
  • 监控 Java 应用程序上的锁争用

    我正在尝试创建一个小基准 在 Groovy 中 以显示几个同步方法上的高线程争用 当监控自愿上下文切换时 应该会出现高争用 在 Linux 中 这可以通过 pidstat 来实现 程序如下 class Res private int n s
  • 如何在Java中跨类共享变量,我尝试了静态不起作用

    类 Testclass1 有一个变量 有一些执行会改变变量的值 现在在同一个包中有类 Testclass2 我将如何访问 Testclass2 中变量的更新值 由 Testclass1 更新 试过这个没用 注意 Testclass1和Tes

随机推荐