Mule - how to access files in src/main/resources in Java class when running in Studio and Standalone?

StackOverflow https://stackoverflow.com/questions/23214875

Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

Generally when you place in path in src/main/resources it comes under classpath ... In Standalone also it should get it from there ... if not, then could you place it in standalone conf folder where all properties files are kept and have a try

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top