以编程方式查找并加载jfxrt.jar启动一个坏主意?

时间:2014-05-23 07:26:42

标签: java javafx

我有一个包含一些JavaFX代码的swing应用程序。我完全知道jfxrt.jar存在于lib而不是ext目录中的问题,默认情况下不可用。

给出:
 *我不想使用JavaFX Maven插件
 *我不想捆绑一个jre
 *我很高兴以编程方式解决没有Java Fx的系统(我只使用Java FX进行一些图表)

在我的应用程序启动时运行以下代码是否有任何问题?我已经在windows,mac和linux上使用各种jre测试了它,并且没有捆绑Java FX jar(OpenJDK / Oracle / Java7 / Java6组合等)。

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Logger;

public class JavaFxJarLoader
{
final static Logger logger = Logger.getLogger(JavaFxJarLoader.class.getName());

private static final String JFXRT_JAR = "jfxrt.jar";
private static final String JRE_LIB_SUFFIX = File.separator + "lib" + File.separator + "ext";

public static boolean javaFXAvailable()
{
    boolean wasFound = true;
    try
    {
        Class.forName("javafx.scene.chart.PieChart");
    }
    catch(Exception e)
    {
        wasFound = false;
    }
    return wasFound;
}

public static void tryToMakeJavaFXAvailable()
{
    if ( javaFXAvailable())
    {
        logger.info("Java FX availble by default, no action taken");
    }
    else
    {
        final File javaFxJarFile = findJavaFxJarFile();

        if ( javaFxJarFile != null )
        {
            try
            {
                addURLToSystemClassLoader(javaFxJarFile.toURI().toURL());
            } 
            catch (MalformedURLException e)
            {
                logger.warning(e.toString());
            } 
        }
        else
        {
            logger.info("Failed to find " + JFXRT_JAR + ", Java Fx not available on this system.");
        }
    }
}

// Uses java.ext.dirs to find the jfxrt.jar file.
private static File findJavaFxJarFile()
{
    File ret = null;
    final String javaExtDirs = System.getProperty("java.ext.dirs");

    logger.info("Seraching for " + JFXRT_JAR + ", using java.ext.dirs = '" + javaExtDirs + "'");

    if ( javaExtDirs != null )
    {
        final String dirs[] = javaExtDirs.split(File.pathSeparator);
        for (int i = 0; i < dirs.length; i++)
        {
            final String dir = dirs[i];

            if ( dir.endsWith(JRE_LIB_SUFFIX))
            {
                final String jfxPath = dir.substring(0, dir.length()-3) + JFXRT_JAR;
                final File jfxFile = new File(jfxPath);

                final boolean selected = jfxFile.exists();
                logger.info("Candidate " + JFXRT_JAR + " in " + jfxFile.toString() + " " + (selected ? "will be used" : "ignored, does not exists"));
                if ( selected )
                {
                    ret = jfxFile;
                    break;
                }
            }
        }
    }
    return ret;
}

private static void addURLToSystemClassLoader(URL url) 
{ 
    final URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); 
    final Class<URLClassLoader> classLoaderClass = URLClassLoader.class; 

    try 
    { 
        final Method method = classLoaderClass.getDeclaredMethod("addURL", new Class[]{URL.class}); 
        method.setAccessible(true); 
        method.invoke(systemClassLoader, new Object[]{url});
    } 
    catch (Throwable t) 
    { 
        logger.warning("Failed to add " + url.toString() + " to class path: "  + t.toString());
    } 
}

public static void main(String[] args)
{
    logger.warning("Java Fx available before ? " + javaFXAvailable());
    tryToMakeJavaFXAvailable();
    logger.warning("Java Fx available after ? " + javaFXAvailable());
}
}

0 个答案:

没有答案