Java INIT=RUNSCRIPT 和相对路径的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4490138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:59:54  来源:igfitidea点击:

problem with INIT=RUNSCRIPT and relative paths

javahibernatemaven-2configurationh2

提问by Maxime ARNSTAMM

I use maven conventions for source paths (src/main src/test) and i have my sql scripts in src/main/resources/scripts.

我对源路径(src/main src/test)使用 maven 约定,并且我在 src/main/resources/scripts 中有我的 sql 脚本。

I want to run my app with H2 memory and i'd like to use the jdbc url to initialize my db :

我想用 H2 内存运行我的应用程序,我想使用 jdbc url 来初始化我的数据库:

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/main/resources/scripts/create.sql';

My problem is that this relative path (src/main/... ) does not work, and that H2 won't crash if the init=runscript command targets nothing.

我的问题是这个相对路径 (src/main/...) 不起作用,如果 init=runscript 命令没有目标,H2 不会崩溃。

Does someone know what is the path i should use to make this work ?

有人知道我应该使用什么路径来完成这项工作吗?

Thanks

谢谢

采纳答案by viktortnk

You can use the following url:
"jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'"

您可以使用以下网址:
"jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'"

With that one it is possible to run script from classpath. So you can just put it src/main/resources/scripts or src/test/resources/scripts in your maven (or something else) project.

有了那个,就可以从类路径运行脚本。所以你可以把它放在 src/main/resources/scripts 或 src/test/resources/scripts 在你的 maven(或其他)项目中。

回答by darioo

I'd suggest trying to use an absolute path for starters, just to check everything works. Afterwards, check your classpath. For example, bin/main/resources/scripts/create.sql, assuming binis where your classes are compiled, and is on your classpath.

我建议尝试为初学者使用绝对路径,只是为了检查一切正常。之后,检查您的类路径。例如,bin/main/resources/scripts/create.sql假设bin是您的类的编译位置,并且位于您的类路径上。

Since src, where your source lives, usually isn't on the classpath, this could be the source of your problem.

由于src,您的源所在的位置通常不在类路径上,因此这可能是您的问题的根源。