簡體   English   中英

使用Webstart 1.6啟動1.4.2_12應用程序時缺少MissingResourceException

[英]MissingResourceException when starting 1.4.2_12 application with webstart 1.6

在我最終弄清楚了為什么JWS 1.6.0_29無法啟動1.4.2_12應用程序(請參閱此問題 )之后,在啟動1.4.2_12應用程序時遇到了另一個異常。 使用JWS 1.6.0_29。

加載ResourceBundle時出現MissingResourceException。 但是message.properties文件確實與正在加載它的類存在於同一個程序包中。

當使用JWS 1.4或1.5啟動應用程序時,不會引發異常。
僅在啟動應用程序時引發異常。 使用JWS 1.6。

完整的堆棧跟蹤為:

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javaws.Launcher.executeApplication(Unknown Source)
    at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
    at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
    at com.sun.javaws.Launcher.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.util.MissingResourceException: Can't find bundle for base name com.test.hello.messages, locale fr_FR
    at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
    at java.util.ResourceBundle.getBundleImpl(Unknown Source)
    at java.util.ResourceBundle.getBundle(Unknown Source)
    at com.test.hello.Main.<clinit>(Main.java:10)
    ... 9 more

重現測試用例

JNLP描述符是:

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://localhost:80/meslegacy/apps" href="testJwsXXTo142.jnlp">
    <information>
        <title>JWS TEST 1.6 -> 1.4.2</title>
        <vendor>Hello World Vendor</vendor>
        <description>Hello World</description>
    </information>

    <security>
        <all-permissions />
    </security>

    <resources>
        <j2se version="1.4.2_12" href="http://java.sun.com/products/autodl/j2se" />
        <jar href="jar/helloworld.jar" main="true" />
    </resources>

    <application-desc main-class="com.test.hello.Main" />
</jnlp>

com.test.hello.Main類為:

package com.test.hello;

import java.util.ResourceBundle;

import javax.swing.JFrame;

public class Main {

    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(Main.class.getPackage().getName()+".messages");

    public static void main(String[] args) {
        JFrame frame = new JFrame("Hello world !");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        frame.setVisible(true);
    }

}

補充測試

  • 在ResourceBundle.getBundle()方法中指定ClassLoader和Locale不能解決問題。
  • Main.class.getClassLaoder()和Thread.currentThread()。getContextClassLaoder()已經過測試,並產生了相同的異常。
  • “手動”加載資源確實有效(請參見下文)。

測試代碼以手動加載資源:

ClassLoader cl = Main.class.getClassLoader();
String resourcePath = baseName.replaceAll("\\.", "/");
System.out.println(resourcePath);
URL resourceUrl = cl.getResource(resourcePath+".properties");
System.out.println("Resource manually loaded :"+resourceUrl);

將產生:

com/test/hello/messages.properties
Resource manually loaded :jar:http://localhost:80/meslegacy/apps/jar/helloworld.jar!/com%2ftest%2fhello%2fmessages.properties
  • 但是,雖然有可能找到資源,但沒有獲得資源內容。

例:

ClassLoader cl = Main.class.getClassLoader();
String resourcePath = baseName.replaceAll("\\.", "/") + ".properties";
URL resourceUrl = cl.getResource(resourcePath);
// here, resourceUrl is not null. Then build bundle by hand
ResourceBundle prb = new PropertyResourceBundle(resourceUrl.openStream());

產生的:

java.io.FileNotFoundException: JAR entry com%2ftest%2fhello%2fmessages.properties not found in C:\Documents and Settings\firstname.lastname\Application Data\Sun\Java\Deployment\cache\6.0\18\3bfe5d92-3dfda9ef
    at com.sun.jnlp.JNLPCachedJarURLConnection.connect(Unknown Source)
    at com.sun.jnlp.JNLPCachedJarURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at com.test.hello.Main.main(Main.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javaws.Launcher.executeApplication(Unknown Source)
    at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
    at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
    at com.sun.javaws.Launcher.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

似乎更多是一種緩存問題...

各位,我有一個提示,不勝感激,

謝謝閱讀。

這是此問題的解釋和解決方法。

1-說明

問題來自系統ClassCloader(JWS6系統ClassLoader)返回的URL。

在JWS 1.6中,系統ClassLoader返回的URL包含轉義序列,如以下所示:

jar:http://localhost:80/meslegacy/apps/jar/helloworld.jar!/com%2ftest%2fhello%2fmessages.properties

可以在類路徑中定位資源,但是當涉及到實際訪問該資源的內容時,會引發FileNotFoundException:這是導致ResourceBundle中FileNotFoundException的原因。

請注意,當URL中沒有出現轉義序列時(例如,當資源位於claspath的根目錄時),訪問資源內容沒有問題。 僅當您在URL路徑部分中獲得%xx內容時,問題才會出現。

2-Workarround

一旦解決了問題(花了我幾天的時間弄清楚!),就該找一個工作環境了。 雖然我可以在特定的本地化代碼部分上解決我的問題,但很快發現,可以通過編碼特定的ClassLoader以“替換” JNLPClassLoader來全局解決問題。 我不會嚴格地“替換”,因為這對我來說似乎是不可能的,但我寧願執行以下操作:

  1. 禁用SecurityManager使其能夠與我的自定義類加載器一起玩
  2. 編寫我自己的從URLClassLoader派生的類加載器,該類加載器在返回URL時會修復URL
  3. 使用從JNLPClassLoader提取的claspath設置其類路徑
  4. 將此自定義類加載器設置為上下文類加載器
  5. 將此自定義類加載器設置為AWT-Event-Thread上下文類加載器
  6. 使用此自定義類加載器加載我的應用程序入口點。

這提供了以下ClassLoader

public class JwsUrlFixerClassLoader extends URLClassLoader {

    private final static Logger LOG = Logger.getLogger(JwsUrlFixerClassLoader.class);

    private static String SIMPLE_CLASS_NAME = null;

    private static boolean LOG_ENABLED = "true".equals(System.getProperty("classloader.debug"));

    static {
        SIMPLE_CLASS_NAME = JwsUrlFixerClassLoader.class.getName();
        int idx = SIMPLE_CLASS_NAME.lastIndexOf('.');
        if (idx >= 0 && idx < SIMPLE_CLASS_NAME.length()-1) {
            SIMPLE_CLASS_NAME = SIMPLE_CLASS_NAME.substring(idx + 1);
        }
    }

    public JwsUrlFixerClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

    public URL getResource(String name) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getResource(): getResource(" + name + ")");
        }
        if (LOG_ENABLED) {
            login("getResource(" + name + ")");
        }
        URL out = super.getResource(name);
        if (out != null) {
            out = URLFixerTool.fixUrl(out);
        }
        if (LOG_ENABLED) {
            logout("getResource returning " + out);
        }
        return out;
    }

    public URL findResource(String name) {
        if (LOG_ENABLED) {
            login("findResource(" + name + ")");
        }
        URL out = super.findResource(name);
        if (out != null) {
            out = URLFixerTool.fixUrl(out);
        }
        if (LOG_ENABLED) {
            logout("findResource returning " + out);
        }
        return out;
    }

    public InputStream getResourceAsStream(String name) {
        if (LOG_ENABLED) {
            login("getResourceAsStream(" + name + ")");
        }
        InputStream out = super.getResourceAsStream(name);
        if (LOG_ENABLED) {
            logout("getResourceAsStream returning " + out);
        }
        return out;
    }

    protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
        if (LOG_ENABLED) {
            login("loadClass(" + name + ")");
        }
        // First, check if the class has already been loaded
        Class c = findLoadedClass(name);
        if (c == null) {
            try {
                c = findClass(name);
            } catch (ClassNotFoundException cnfe) {
                if (getParent() == null) {
                    // c = findBootstrapClass0(name);
                    Method m = null;
                    try {
                        m = URLClassLoader.class.getMethod("findBootstrapClass0", new Class[] {});
                        m.setAccessible(true);
                        c = (Class) m.invoke(this, new Object[] { name });
                    } catch (Exception e) {
                        throw new ClassNotFoundException();
                    }
                } else {
                    c = getParent().loadClass(name);
                }
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        if (LOG_ENABLED) {
            logout("loadClass returning " + c);
        }
        return c;
    }

    private static void login(String message) {
        System.out.println("---> [" + Thread.currentThread().getName() + "] " + SIMPLE_CLASS_NAME + ": " + message);
    }

    private static void logout(String message) {
        System.out.println("<--- [" + Thread.currentThread().getName() + "] " + SIMPLE_CLASS_NAME + ": " + message);
    }

}

現在,在我設置為JNLP描述符中的主類的AppBoostrap類中,執行以下操作:

    System.setSecurityManager(null);
    ClassLoader parentCL = AppBootstrap.class.getClassLoader();
    URL[] classpath = new URL[] {};
    if (parentCL instanceof URLClassLoader) {
        URLClassLoader ucl = (URLClassLoader) parentCL;
        classpath = ucl.getURLs();
    }
    final JwsUrlFixerClassLoader vlrCL = new JwsUrlFixerClassLoader(classpath, parentCL);
    Thread.currentThread().setContextClassLoader(vlrCL);
    try {
        SwingUtilities.invokeAndWait(new Runnable() {

            public void run() {
                Thread.currentThread().setContextClassLoader(vlrCL);
            }
        });
    } catch (Exception e) {
        LOG.error("main(): Failed to set context classloader !", e);
    }

在上一個摘錄中,我獲得了ClassLoader,該類加載了AppBootstrap類,並將其用作JwsUrlFixerClassLoader的父類加載器。

我必須解決URLClassLodaer.loadClass()的默認父級委派策略的問題,並將其替換為“先嘗試我的類路徑,然后再嘗試父級”。

完成此操作后,一切正常,並且其他一些我們迄今無法解釋的錯誤消失了。

太神奇了! 經過很多痛苦之后...

希望有一天能對某人有所幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM