Search code examples
javamulefilepathmule-studioflatpack

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.


Solution

  • 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.