Java getDefaultToolKit() hangs Mac OS X 10.5

2019-08-22 07:00发布

问题:

I am using Eclipse Cocao on Mac OS X 10.5.7, and the program hangs on the innocent line below while initializing variables. If I take it out, the program proceeds.

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

This line works fine in XP, Vista, Server 2003, and Mac OS X 10.4.11. I have no idea why it infinitely hangs now in 10.5. Any ideas?

Update: This appears to me to be a bug when using Cocoa Eclipse and Cocoa SWT. I can reproduce the example from Thorbjørn below without any problem. When I run the test program here though, I get the hanging behavior.

import java.awt.Dimension;
import org.eclipse.swt.widgets.Display;

public class Foo {
  static Display display = new Display();

  public Foo() {
    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    System.out.println(screenSize);
  }

  public static void main(String[] args) {
    Foo test = new Foo();
  }
} 

回答1:

The following code completes with Eclipse Cocoa 3.5 under OS X 10.5 on a MacBook with both Java 1.5 and Java 6.

package main;

import java.awt.Dimension;

public class Foo {

    public static void main(String[] args) {
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
                .getScreenSize();
        System.out.println(screenSize);
    }
}

You will have to provide more details about how your system is different from mine.



回答2:

Launch your application with the -Djava.awt.headless=true JVM argument.