views:

342

answers:

3

I mean, how does Java decide which protocols are available? I run some code from inside Eclipse, and it works just fine. Then I run the same code from outside Eclipse, and I get "unknown protocol" MalformedURLException. Probably it has to do with the code base, or something? Any hints would be helpful.

Thanks!

+1  A: 

The work of resolving the protocol is done by the URLStreamHandler, which are stored in URL.handlers by protocol in lowercase. The handler, in turn, is created by the URLStreamHandlerFactory at URL.factory. Maybe eclipse is monkeying with that?

Some of the URL constructors take stream handlers and you can set the factory with URL.setURLStreamHandlerFactory.

Here's a web post about developing protocol handlers.

sblundy
The URL is some custom URL ("media:///") which should be handled by the application.
Gabi
You are definitely right. I could locate the code that does just what you say. Now it all makes sense. Thank you!
Gabi
A: 

Probably a classpath issue. If you are using a protocol that depends on some library (jar) you included, and then exported a JAR from eclipse, the JAR files you included in your project are probably not being found by the running code outside of eclipse. You need a manifest file in your jar that will point to the libraries that are needed.

danivovich
A: 

The java standard way of defining protocol handlers is described here: http://java.sun.com/developer/onlineTraining/protocolhandlers/

This relies on the protocol handler class being available on the boot (?) classloader. That doesn't work well with OSGi (and thus Eclipse). OSGi provides a wrapper around this mechanism to allow bundles/plugins to contribute protocol handlers. See: http://www.osgi.org/javadoc/r4v41/org/osgi/service/url/URLStreamHandlerService.html

Eclipse also provides its own protocol: bundle-resource (iirc) which definitely won't work outside of Eclipse.

Tom