[英]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: 有几种方法可以做到这一点:
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”);
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.