[英]How can i access files in src/main/resources dynamically when the project is a .jar or in IDE
I have a project that uses files found in src/main/resources*/input* and generates files in src/main/resources*/output*.我有一个项目使用 src/main/resources*/input* 中的文件并在 src/main/resources*/output* 中生成文件。
I often used to use我经常用
System.getProperty("user.dir") + "/src/main/resources"
My issue with this is that it is not dynamic.我的问题是它不是动态的。 It works fine when I use my IDE, but when I build my project as a.jar file, I end up having to add src/main/resources to the root where the jar is located.....which is probably bad.
当我使用我的 IDE 时它工作正常,但是当我将我的项目构建为 .jar 文件时,我最终不得不将 src/main/resources 添加到 Z68995FCBF432492D15484DAC40A 所在的根目录.....
I have tried using the following which returns null pointer exception我尝试使用以下返回 null 指针异常
this.getClass().getResource("input").getPath().toString()
Can someone please provide me with a dynamic way to access these directories that I need that are inside src/main/resources in a dynamic way that works from the IDE and from the.jar ?有人可以为我提供一种动态方式来访问我需要的这些目录,这些目录在 src/main/resources中以动态方式从 IDE 和 .jar 工作吗?
You could access the files from resource folder using:您可以使用以下方式访问资源文件夹中的文件:
this.getClass().getResourceAsStream("/NLP/models/en-lemmatizer.bin").toString();
NLP is a folder in resource, which in turn has a models folder with bin file inside it. NLP 是资源中的一个文件夹,其中又包含一个模型文件夹,其中包含 bin 文件。
As per my knowledge, you cannot generate files or create new files inside a jar.据我所知,您不能在 jar 中生成文件或创建新文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.