1

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.

ycr
  • 12,828
  • 2
  • 25
  • 45
  • Do you get another prompt after the two warnings.... exactly what do you mean "system hangs"? And did you run the commands exactly as posted here? – Elliott Frisch Jul 28 '18 at 01:59
  • Hi @ElliottFrisch thanks for the prompt reply, I have updated the question. – ycr Jul 28 '18 at 02:29
  • **And** you silently swallow any exceptions because? Does this code work anywhere? – Elliott Frisch Jul 28 '18 at 02:34
  • It doesn't throw a IO exception, I have added a log and checked. Also this code works fine in my local machine. – ycr Jul 28 '18 at 02:41
  • Be sure that your `$LANG` and `$LANGUAGE` environment variables are set, and you have the necessary stuff to support said settings. You may gain some insight from `man locale` – ivanivan Jul 28 '18 at 03:27

1 Answers1

0

I hit the same JavaFX issue on Ubuntu server using xvfb as the display manager for my UI tests, the root cause for me was my forwarded DISPLAY wasn't injected into dbus's activation environment, so anything that dbus activates that tries to present a UI failed to connect to a display and resulted in the "Unable to open DISPLAY" exception.

Running dbus-update-activation-environment --systemd DISPLAY XAUTHORITY in that shell before launching the UI tests fixed this issue for me.

DeadChex
  • 4,379
  • 1
  • 27
  • 34