I have a simple Application which generates a png of a chart which is based on JavaFX. The app fails to run on displayless machine with the following exception, I don't need to render or display content on the console, just need to create the image.
"main" java.lang.UnsupportedOperationException: Unable to open DISPLAY
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:68)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at javafx.embed.swing.JFXPanel.initFx(JFXPanel.java:215)
at javafx.embed.swing.JFXPanel.<init>(JFXPanel.java:230)
I'm trying to run this on a AWS instance. Is there a way to overcome this issue? Following is my sample code.
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class Test {
public static void main(String[] args) {
String chartGenLocation = "/Users/tmp";
new JFXPanel();
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Failed", 10),
new PieChart.Data("Skipped", 20));
final PieChart chart = new PieChart(pieChartData);
chart.setAnimated(false);
Platform.runLater(() -> {
Stage stage = new Stage();
Scene scene = new Scene(chart, 500, 500);
stage.setScene(scene);
WritableImage img = new WritableImage(500, 500);
scene.snapshot(img);
File file = new File(Paths.get(chartGenLocation, "a.png").toString());
try {
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", file);
} catch (IOException e) {
//logger.error("Error occurred while writing the chart image
}
});
}
}
I have seen few SO answers which mostly talk about monocle and testfx, here I'm unable to add external dependencies. So adding testfx is not a option. I have also tried the following with xvbf, which hangs the system,
Xvfb :95 -screen 0 1024x768x16 &> xvfb.log &
export DISPLAY=:95.0
When I execute I see the following output and system hangs there.
(process:13112): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Fontconfig warning: ignoring UTF-8: not a valid region tag
Update
Execution Sequence
Xvfb :92 -screen 0 1024x768x16 &> xvfb.log &
export DISPLAY=:92.0
No errors in xvbf.log, seems to start properly.
java Test
I see following in console out
(process:13356): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.
Fontconfig warning: ignoring UTF-8: not a valid region tag
I do not see any log in xvbf.log, the execution doesn't proceed after the above log. My image is not getting generated.
Update 2
I would like to know if there is a way to bypass this validation since I really don't need a display rendering.