Search code examples
javaglassfishawtheadless

Drawing an image on a headless server


I try to draw an image on a headless server - imagine something like a map with a marker put on it - but nothing happens, not even an exception. I already tried to start the application server with the headless=true parameter but it didn't work. Any ideas?

public BufferedImage drawOverlay(BufferedImage map, int x, int y) {
    BufferedImage combination = null;

    try {
        InputStream is = FacesContext
                .getCurrentInstance()
                .getExternalContext()
                .getResourceAsStream("/img/marker.png");
        BufferedImage overlay = ImageIO.read(is);

        int mapWidth = map.getWidth();
        int mapHeight = map.getHeight();

        int overlayWidth = overlay.getWidth();
        int overlayHeight = overlay.getHeight();

        int width = Math.max(mapWidth, overlayWidth);
        int height = Math.max(mapHeight, overlayHeight);

        combination = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = combination.getGraphics();

        g.drawImage(map, 0, 0, null);
        g.drawImage(overlay, x, y, null);
    }
    catch (IOException ex) {
        LOGGER.log(Level.SEVERE, "Couldn't draw overlay");
    }
    return combination;
}

Solution

  • If the server is linux or unix usually need an X server running to AWT work.

    It's ussually to start a virtual X server in linux with the command

    $ Xvfb :99
    

    And then, you must start the java server with the DISPLAY environment variable

    export DISPLAY=:99