简体   繁体   中英

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. My Java class needs to access a flatpack XML file called map.xml - I have placed this in src/main/resources . My Java class is in src/main/java . I'm currently accessing the map.xml file in my Java class as follows:

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:

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? I have also tried simply fr = new FileReader("map.xml"); and this fails in Studio.

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. I've updated my Java class to the following and this has worked in both Studio and Standalone:

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

UPDATE

How to retieve mule-app.properties file? If same then will it work onCloudHub as well.

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

    InputStream is = 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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