Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all supported URL schemes in Java8

Tags:

java

url

Is there any way to print all supported URL schemes in Java8? I know Java8 supports http, https, file etc. but what else is supported and will be correctly processed by URL.openStream() method?

like image 587
CaptainRR Avatar asked Jan 21 '17 21:01

CaptainRR


1 Answers

The javadoc for java.net.URL states:

Protocol handlers for the following protocols are guaranteed to exist on the search path : http, https, file, and jar

Protocol handlers for additional protocols may also be available.

If you want more than http, https, file, and jar, you need to configure them yourself by writing and loading a Protocol Handler.

  • If the application has previously set up an instance of URLStreamHandlerFactory as the stream handler factory, then the createURLStreamHandler method of that instance is called with the protocol string as an argument to create the stream protocol handler.

  • If no URLStreamHandlerFactory has yet been set up, or if the factory's createURLStreamHandler method returns null, then the constructor finds the value of the system property: java.protocol.handler.pkgs

  • If the value of that system property is not null, it is interpreted as a list of packages separated by a vertical slash character '|'. The constructor tries to load the class named <package>.<protocol>.Handler

....where <package> is replaced by the name of the package and <protocol> is replaced by the name of the protocol. If this class does not exist, or if the class exists but it is not a subclass of URLStreamHandler, then the next package in the list is tried.

  • If the previous step fails to find a protocol handler, then the constructor tries to load from a system default package : <system default package>.<protocol>.Handler
like image 198
Fabien Benoit-Koch Avatar answered Oct 16 '22 11:10

Fabien Benoit-Koch