3

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.

david a.
  • 5,283
  • 22
  • 24
danw
  • 1,608
  • 4
  • 29
  • 48
  • I used this path, but I am getting "/Users/userName/appName/.mule/apps/appName/classes/liquibase/db.changelog.xml does not exist" but when I look at the path in the file explorer, its there. – Nathan Tregillus May 12 '16 at 16:06

2 Answers2

3

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.

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
  • Thanks @Learner. I explored your first option and have managed to find a solution. I'v updated my original post. – danw Apr 22 '14 at 14:24
0

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

Anirban Sen Chowdhary
  • 8,233
  • 6
  • 39
  • 81
  • Thanks @Anirban. Would you be able to provide an example file path of how I'd access something in the classpath? – danw Apr 22 '14 at 09:07
  • In my case I have put the file in src/main/resources and just referred it as "abc.xml" ... need not to give as src/main/resources ... just "abc.xml" will work fine ... if you created any folder say conf in src/main/resources then you need to provide "conf/abc.xml" – Anirban Sen Chowdhary Apr 22 '14 at 15:14