Java - 注册自定义URL协议处理程序 [英] Java - Registering custom URL protocol handlers

查看:126
本文介绍了Java - 注册自定义URL协议处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为类路径协议注册自定义URL处理程序,如另一个主题。以下是代码:

I tried to register a custom URL handler for a classpath protocol, as described in another thread. Here is the code:

package com.mycompany;

import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import com.mycompany.protocol.classpath.Handler;

public class ParserTest {
    @Test
    public void testParsing() throws MalformedURLException {      
        System.out.println(System.getProperty("java.protocol.handler.pkgs"));

        //URL url = new URL(null, "classpath://com.mycompany/hello-world.xml", new Handler(ClassLoader.getSystemClassLoader()));
        URL url = new URL("classpath://com.mycompany/hello-world.xml");
    }
}

测试用例具有以下JVM参数:

The test case has the following JVM arguments:

-Djava.protocol.handler.pkgs=com.mycompany.protocol

System.out.println 行正确输出 com.mycompany.protocol ,因此正在设置该属性。但是,看起来它没有生效,因为上面的调用会抛出一个 java.net.MalformedURLException:unknown protocol:classpath exception。

The System.out.println line properly outputs com.mycompany.protocol, so the property is being set. However, it looks like it's not being taken into effect, because the above call will throw a java.net.MalformedURLException: unknown protocol: classpath exception.

如果我在注释行中明确提供处理程序,一切都很好。但是,我宁愿不明确地提供它 - 它应该自动完成。

If I provide the handler explicitly as in the commented line, everything is fine. However, I would rather not provide it explicitly - it should be done automatically.

我做错了什么?

推荐答案

我发现了这个问题。我使用的原始类路径处理程序类有一个非默认构造函数。当然,因为它只有一个非默认的构造函数,所以处理程序无法实例化。我向所有尝试调试此问题的人道歉,我没有看到这种联系。

I have found the issue. The original classpath handler class that I used had a non-default constructor. Of course, because it had only a non-default constructor, the handler couldn't be instantiated. I apologize to everyone who have tried to debug this issue, I failed to see this connection.

这篇关于Java - 注册自定义URL协议处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆