I’m using JFreeChart to generate some reporting in my web application, I use JFreechart to build the chart and save it as an image, but I have never found that image in my Tomcat directory.
I tried to print the working directory, and it gives me the Tomcat root, so I’m wondering why the image is never written in that directory.
Here is my Service method:
public DefaultPieDataset generateChartEquipementsByDepartementDataset(){
DefaultPieDataset pieDataset ;
pieDataset = convertMapToDataset(reportsDao.getEquipementsByDepartementDataset()) ;
JFreeChart chart = ChartFactory.createPieChart(
"% Equipements par departement",
pieDataset,
true,
true,
false
) ;
try{
ChartUtilities.saveChartAsJPEG(new File("/chart.jpeg"), chart, 400, 400) ;
}catch(Exception ex){}
return pieDataset ;
}
When I test it, it writes the image in the root of my application, but never in tomcat.
Here is the controller.
@RequestMapping(value="/reports")
public String reportsHandler( Model model ) {
reportingService.generateChartEquipementsByDepartementDataset() ;
String curSession = System.getProperty("user.dir") ;
model.addAttribute("curSession", curSession) ;
return "home" ;
}
Root (
/) doesn’t point to the root path of the web app, you need to get a “translated path”, by using ServletContext.getRealPath(), like