简体   繁体   English

当项目为 a.jar 或 IDE 时,如何动态访问 src/main/resources 中的文件

[英]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.

相关问题 如何从IDE和JAR访问src / main / resources - How to access src/main/resources from IDE and JAR Maven 在构建 jar 时损坏 src/main/resources 中的二进制文件 - Maven corrupting binary files in src/main/resources when building jar Mule - 在Studio和Standalone中运行时如何访问Java类中src / main / resources中的文件? - Mule - how to access files in src/main/resources in Java class when running in Studio and Standalone? 如何使用@ImportResource导入src / main / resources / static中的配置文件的整个目录? - How can I use @ImportResource to import an entire directory of config files in src/main/resources/static? 使用mvn exec:java时,如何将src / main / resources添加到类路径中 - How can I add src/main/resources to the classpath when using mvn exec:java 从源代码动态访问src / main / resources / - Access src/main/resources/ dynamically from source code src / main / resources中的Maven Jar主类 - Maven Jar main class in src/main/resources 无法通过测试用例访问src / main / resources中的文件 - Can't access files from src/main/resources via a test-case Maven项目不会将文件放在classpath的src / main / resources中 - Maven project doesn't put files in src/main/resources on classpath 如何在运行时从JSF控制器访问位于“ src / main / resources”文件夹中的css文件 - How can I access a css file located in my “src/main/resources” folder from my JSF controller at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM