简体   繁体   English

将文件夹添加到类路径

[英]Add folder to classpath

I have a the following source structure 我有以下源代码结构

/src/main/java
/src/main/resources
/src/main/resources/stuff
/src/main/resources/stuff/1.txt
/src/main/resources/stuff/2.txt

In my code I access the files in the stuff folder via: 在我的代码中,我通过以下方式访问stuff文件夹中的文件:

ClassLoader.class.getResource("/stuff/1.txt").getFile()

When this is deployed it is deployed like this: 部署后,其部署方式如下:

/
/lib
/lib/my-app.jar
/lib/other.jar
/lib/stuff

Currently my classpath is set to: lib/* but I can't resolve lib/stuff/1.txt in the way I do above. 目前,我的类路径设置为: lib/*但是我无法以上述方式解析lib/stuff/1.txt What do I need to set on the classpath for the above code to work? 为了使上述代码正常工作,我需要在类路径上设置什么?

It might be the difference between using relative and absolute paths as pointed out in this blog . 本博客所述,使用相对路径和绝对路径可能有所不同。

ClassLoader.class.getResource("lib/stuff/1.txt").getFile()

As pointed out by @Gimby, it'd be more efficient to use getResourseAsStream() . 正如@Gimby指出的那样,使用getResourseAsStream()会更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM