0

I have a JFreeChart plot that I would like to save as a PNG file from a Java swing application.

XYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(new Color(255,228,196));
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
panel.setSize(600, 300);

JFileChooser exportChooser = new JFileChooser();
exportChooser.setSelectedFile(new File("test plot"));
FileFilter filter = new FileNameExtensionFilter("PNG File","png");
exportChooser.setFileFilter(filter);
int returnVal = exportChooser.showDialog(null, "Export");
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = exportChooser.getSelectedFile();
try {
    OutputStream out = new FileOutputStream(file);
    ChartUtilities.writeChartAsPNG(out, chart, panel.getWidth(), panel.getHeight());
}catch (IOException e) {
  e.printStackTrace();
}catch (Throwable t){
  t.printStackTrace();
}
}

I got this error:

java.lang.UnsatisfiedLinkError:
javax.imageio.ImageIO.write(Ljava/awt/image/RenderedImage;Ljava/lang/String;Ljava/io/OutputStream;)

I am not sure what this means or how to resolve it. I am also not sure if my code makes sense, specifically OutputStream out = new FileOutputStream(file);. Would really appreciate any insights!

  • Why not `ChartUtilities.saveChartAsPNG()`? – trashgod Nov 04 '18 at 11:11
  • @trashgod the error still persisted.... – A BeauTifful Life Nov 04 '18 at 15:47
  • Works for me; a missing library sounds like a `JFreeChart` installation issue, mentioned [here](https://stackoverflow.com/a/53145666/230513), or a Java installation issue. – trashgod Nov 04 '18 at 21:34
  • What Java version are you running? What is the output of `java --version` in the runtime environment that you get the error? Seems like a JRE issue. – Harald K Nov 05 '18 at 11:56
  • @haraldK java 10.0.2 Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode) – A BeauTifful Life Nov 05 '18 at 14:28
  • Weird. Are you sure the `java.desktop` module is part of this JRE? It's now possible to include/exclude modules, and create a tailored, minimal JRE exactly for your needs, using the `jlink` tool. – Harald K Nov 05 '18 at 14:35
  • @haraldK I could comprehend your comment a bit only after reading an article about Java 9's modularity feature (sorry for the late response). But how do I make sure it's part of the JRE? I also decided to try ImageIO but ran into a method-not-found error; I made a post https://stackoverflow.com/questions/53230998/imageio-found-in-launcher-but-not-elsewhere. Don't know if what you were talking about is related? – A BeauTifful Life Nov 09 '18 at 18:04

0 Answers0