Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java's URL class not recognize certain protocols?

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 don't understand why such exceptions arise..

like image 276
trinity Avatar asked Mar 09 '10 03:03

trinity


People also ask

Which class allows you to retrieve the details of URL?

A protocol handler is a Java class that implements the communications protocol for accessing the URL resource. For example, given an http URL, Java prepares to use the HTTP protocol handler to retrieve documents from the specified server.

Which method of URL class represents a URL and has complete set of methods to manipulate URL in Java?

Constructors. The java. net. URL class represents a URL and has a complete set of methods to manipulate URL in Java.

What are the methods in the URL class used for parsing the URL?

The URL class provides several methods that let you query URL objects. You can get the protocol, authority, host name, port number, path, query, filename, and reference from a URL using these accessor methods: getProtocol.


2 Answers

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.

like image 176
Ben S Avatar answered Oct 05 '22 23:10

Ben S


Sounds like there's no registered handler for the protocol "telnet" in your application. Since the URL class can be used to open a InputStream to URL it needs to have a registered handler for the protocol to do this work if you're to be allowed to create an object using it.

For details on how to add handlers see: http://docs.oracle.com/javase/7/docs/api/java/net/URLStreamHandlerFactory.html

like image 43
Ian C. Avatar answered Oct 06 '22 00:10

Ian C.