JDK-7181027 : [macosx] Unable to use headless mode
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2012-07-02
  • Updated: 2012-08-14
  • Resolved: 2012-08-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u6 b22Fixed 8Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Darwin dhcp-36.trollworks.com 11.4.2 Darwin Kernel Version 11.4.2: Wed May 30 20:13:51 PDT 2012; root:xnu-1699.31.2~1/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
This is effectively a duplicate of bug 7144064. However, that bug was marked as fixed in 7u4, which doesn't seem to be the case. My particular situation can be demonstrated with the following:

1) Connect to an OSX machine via ssh. DISPLAY is not set.

2) try the following code:

import java.awt.*;
import java.awt.image.*;

public class Test {
    public static final void main(String[] args) {
        System.setProperty("java.awt.headless", Boolean.TRUE.toString());
        BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = img.getGraphics();
    }
}

3) get the following exception:

Exception in thread "main" java.lang.Error: Could not instantiate Graphics Environment: sun.java2d.HeadlessGraphicsEnvironment
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:118)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1152)
	at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1142)
	at Test.main(Test.java:9)

Please note that the same code runs without an exception when executed locally (i.e. no ssh).

REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Connect to an OSX machine via ssh. DISPLAY is not set.

2) try the following code:

import java.awt.*;
import java.awt.image.*;

public class Test {
    public static final void main(String[] args) {
        System.setProperty("java.awt.headless", Boolean.TRUE.toString());
        BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = img.getGraphics();
    }
}

3) get the following exception:

Exception in thread "main" java.lang.Error: Could not instantiate Graphics Environment: sun.java2d.HeadlessGraphicsEnvironment
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:118)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1152)
	at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1142)
	at Test.main(Test.java:9)

Please note that the same code runs without an exception when executed locally (i.e. no ssh).

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No exception thrown.
ACTUAL -
Exception in thread "main" java.lang.Error: Could not instantiate Graphics Environment: sun.java2d.HeadlessGraphicsEnvironment
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:118)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1152)
	at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1142)
	at Test.main(Test.java:9)

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.Error: Could not instantiate Graphics Environment: sun.java2d.HeadlessGraphicsEnvironment
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:118)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:81)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1152)
	at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1142)
	at Test.main(Test.java:9)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.image.*;

public class Test {
    public static final void main(String[] args) {
        System.setProperty("java.awt.headless", Boolean.TRUE.toString());
        BufferedImage img = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = img.getGraphics();
    }
}

---------- END SOURCE ----------

Comments
EVALUATION http://hg.openjdk.java.net/hsx/hotspot-comp/jdk/rev/b1c5e4a843f3
14-08-2012

EVALUATION The problem here is that for headless mode "java.awt.graphicsenv" system property should be CGraphicsEnvironment because the way GraphicsEnvironment.createGE() method works: it first instantiates GraphicsEnvironment instance and then wraps it with HeadlessGraphicsEnvironment if in headless mode.
11-07-2012