简体   繁体   中英

javafx JFXPanel embeded in swing application crash in mac

The jfxpanel in a swing aplication crash once I run in mac. It runs fine in windows but in mac below error come, looks like something to do with font, but not sure why, please help

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.t2k.MacFontFinder.initPSFontNameToPathMap(MacFontFinder.java:339)
    at com.sun.t2k.MacFontFinder.getAllAvailableFontFamilies(MacFontFinder.java:359)
    at com.sun.t2k.T2KFontFactory.getFontFamilyNames(T2KFontFactory.java:1056)
    at com.sun.prism.j2d.J2DFontFactory.getFontFamilyNames(J2DFontFactory.java:52)
    at com.sun.webpane.sg.prism.WCFontImpl.getFont(WCFontImpl.java:37)
    at com.sun.webpane.sg.prism.FXGraphicsManager.getWCFont(FXGraphicsManager.java:56)
    at com.sun.webpane.webkit.network.URLLoader.twkDidFinishLoading(Native Method)
    at com.sun.webpane.webkit.network.URLLoader.access$1300(URLLoader.java:44)
    at com.sun.webpane.webkit.network.URLLoader$6.run(URLLoader.java:691)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
Invalid memory access of location 0x0 rip=0x11c8c7b64
Segmentation fault: 11

You are using a runtime environment that is not compatible with JavaFX.

That is inadvisable. A version of JavaFX compatible with Apple Java Runtime for Mac was never released. You should use Oracle Java 8+ or OpenJDK 8+ if you wish JavaFX for Mac to work correctly. If you cannot use one of those compatible runtimes then use of JavaFX is not recommended.

If you are using Apple Java for Mac because you are worried about a Java runtime for your application being available on the user machine, then consider packaging your application as a self-contained application , which embeds a compatible runtime with your application and does not rely on a pre-installed runtime.

have you tried this dirty hacking? I read somewhere that this problem occurs in JDK 7 on OS X/el Capitan, but that it is very likely that it will never get fixed in JDK 7.

So I found this dirty hack, it works for me ...

    if (isMac()) {
        try {
            final Class<?> macFontFinderClass = Class.forName("com.sun.t2k.MacFontFinder");
            final Field psNameToPathMap = macFontFinderClass.getDeclaredField("psNameToPathMap");
            psNameToPathMap.setAccessible(true);
            if (psNameToPathMap.get(null) == null) {
                psNameToPathMap.set(
                    null, new HashMap<String, String>());
            }
            final Field allAvailableFontFamilies = macFontFinderClass.getDeclaredField("allAvailableFontFamilies");
            allAvailableFontFamilies.setAccessible(true);
            if (allAvailableFontFamilies.get(null) == null) {
                allAvailableFontFamilies.set(
                    null, new String[] {});
            }
        } catch (final Exception e) {
            // ignore
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM