17

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");
    }
}

The test case has the following JVM arguments:

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

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.

What am I doing wrong?

4
  • Does it work if you pass the handler to the URL's constructor? (This is just a debugging proposal, not the final solution). Commented Jun 8, 2011 at 12:16
  • Yes, if I comment out the first statement and use that one instead, it works. Commented Jun 8, 2011 at 12:36
  • Ah, sorry, did not read the question fully :-(. Commented Jun 8, 2011 at 12:37
  • @Dario can you please advise me on how to write a custom jdbc driver? If there is a good reference somewhere, please help me with its link Commented Aug 14, 2018 at 12:33

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

don't be sorry, they will also know how to solve this kind of question.
0

Probably easiest way to debug such problems is to enumerate the protocol handlers registered.

2 Comments

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
The link seems no longer valid (as of 11/03/2014)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.