why does java's URL class not recognize certain protocols?

trinity picture trinity · Mar 9, 2010 · Viewed 20.1k times · Source

URL u=new URL("telnet://route-server.exodus.net");

This line is generating :

java.net.MalformedURLException: unknown protocol: telnet

And i encounter similar problems with other URLs that begin with "news://"

These are URLs extracted from ODP , so i dont understand why such exceptions arise..

Answer

Ben S picture Ben S · Mar 9, 2010

Issue

Java throws a MalformedURLException because it couldn't find a URLStreamHandler for that protocol. Check the javadocs of the constructors for the details.

Summary:

Since the URL class has an openConnection method, the URL class checks to make sure that Java knows how to open a connection of the correct protocol. Without a URLStreamHandler for that protocol, Java refuses to create a URL to save you from failure when you try to call openConnection.

Solution

You should probably be using the URI class if you don't plan on opening a connection of those protocols in Java.