简体   繁体   English

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?

I have a Mule CE application that is using a Java component to transform a CSV file to XML. 我有一个Mule CE应用程序,它使用Java组件将CSV文件转换为XML。 My Java class needs to access a flatpack XML file called map.xml - I have placed this in src/main/resources . 我的Java类需要访问名为map.xml的flatpack XML文件 - 我将它放在src/main/resources My Java class is in src/main/java . 我的Java类在src/main/java I'm currently accessing the map.xml file in my Java class as follows: 我目前正在访问我的Java类中的map.xml文件,如下所示:

fr = new FileReader("src/main/resources/map.xml");

This works fine in Mule Studio, but when I try and run this application in Mule Standalone, I get the following error: 这在Mule Studio中工作正常,但是当我尝试在Mule Standalone中运行此应用程序时,我收到以下错误:

java.io.FileNotFoundException: src/main/resources/map.xml (No such file or directory)

Is there a way I can make this file path mutual so that it will work in both Studio and Standalone? 有没有办法让这个文件路径相互作用,以便它可以在Studio和Standalone中工作? I have also tried simply fr = new FileReader("map.xml"); 我也尝试过简单的fr = new FileReader("map.xml"); and this fails in Studio. 这在Studio中失败了。

UPDATE UPDATE

Through a combination of the answer from @Learner, and some info in this post, I've managed to find a solution to this problem. 通过从@Learner答案,并在一些信息的组合这个帖子,我已经成功地找到解决这个问题。 I've updated my Java class to the following and this has worked in both Studio and Standalone: 我已将我的Java类更新为以下内容,这在Studio和Standalone中都有效:

fr = new FileReader(MyClassName.class.getResource("/map.xml").getPath());

UPDATE UPDATE

How to retieve mule-app.properties file? 如何修复mule-app.properties文件? If same then will it work onCloudHub as well. 如果相同那么它也可以在onCloudHub上运行。

There are couple of ways to do this: 有几种方法可以做到这一点:

  1. You may read resource as stream like this, files under src/main/resources are in your classpath 您可以像这样读取资源,src / main / resources下的文件位于类路径中

    InputStream is = this.getClass().getResourceAsStream("map.xml"); InputStream是= this.getClass()。getResourceAsStream(“map.xml”);

  2. The other recommended way is to create your transformer as a spring bean and inject external map.xml file dependency through spring. 另一种推荐的方法是将变换器创建为spring bean并通过spring注入外部map.xml文件依赖项。

通常,当你在src / main / resources中的路径中放置它时,它来自classpath ...在Standalone中也应该从那里获取它...如果没有,那么你可以将它放在独立的conf文件夹中,其中保存所有属性文件,试试

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

相关问题 从Java中的src / main / resources访问静态文件 - Access static files from src/main/resources in Java 当项目为 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 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 如何包含来自 src/main/java 的资源 - How to include resources from src/main/java 如何在 Grails 3.3.11 的 src/main/java 中访问域 class? - How to access Domain class in src/main/java in Grails 3.3.11? 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java 如何访问存储在 Docker 容器中 'src/main/resources/someFolder' 中的应用程序资源文件? - How to access app resource files stored in 'src/main/resources/someFolder' within a Docker container? Java 无法访问 src/main/resources 中的 txt 文件 - Java can't access txt file in src/main/resources 无法从src / main / resources文件夹Java 8中读取文件 - Cant read files from src/main/resources folder Java 8 运行测试时,从类路径中排除src / main / resources - Exclude src/main/resources from classpath when running tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM