[英]How can I add src/main/resources to the classpath when using mvn exec:java
I'm trying to run the following application which attempts to load a file ( src/main/resources/test.txt
) from the classpath: 我正在尝试运行以下应用程序尝试从类路径加载文件(
src/main/resources/test.txt
):
package com.example;
public class Main {
public static void main(String[] args) {
System.out.println(Main.class.getResource("test.txt"));
}
}
When I do mvn exec:java -Dexec.mainClass=com.example.Main
, I get null
printed out on the command line. 当我执行
mvn exec:java -Dexec.mainClass=com.example.Main
,我在命令行上打印出null
。
So how do I get the files in src/main/resources added to the classpath? 那么如何将src / main / resources中的文件添加到类路径中? Note that I ran
mvn package
, checked the generated target/test.jar
, and confirmed that it included test.txt
at the top level. 请注意,我运行了
mvn package
,检查了生成的target/test.jar
,并确认它在顶层包含了test.txt
。
A slash should solve it 斜线应解决它
System.out.println(Main.class.getResource("/test.txt"));
Your code would work if you placed the test.txt file under: 如果您将test.txt文件放在以下位置,您的代码将起作用:
src/main/resources/com/example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.