Java Reference
In-Depth Information
}
}
The results of this program depend on which virtual machine runs it. Here are the results
from Java 7 on Mac OS X:
http
is
supported
https
is
supported
ftp
is
supported
mailto
is
supported
telnet
is
not
supported
file
is
supported
gopher
is
not
supported
ldap
is
not
supported
jar
is
supported
nfs
is
not
supported
jdbc
is
not
supported
rmi
is
not
supported
doc
is
not
supported
netdoc
is
supported
systemresource
is
not
supported
verbatim
is
not
supported
The nonsupport of RMI and JDBC is actually a little deceptive; in fact, the JDK does
support these protocols. However, that support is through various parts of the
java.rmi
and
java.sql
packages, respectively. These protocols are not accessible through the
URL
class like the other supported protocols (although I have no idea why Sun chose to wrap
up RMI and JDBC parameters in URL clothing if it wasn't intending to interface with
these via Java's quite sophisticated mechanism for handling URLs).
Other Java 7 virtual machines will show similar results. VMs that are not derived from
the Oracle codebase may vary somewhat in which protocols they support. For example,
Android's Dalvik VM only supports the required http, https, file, ftp, and jar protocols.
Constructing a URL from its component parts
You can also build a
URL
by specifying the protocol, the hostname, and the file:
public
URL
(
String
protocol
,
String
hostname
,
String
file
)
throws
MalformedURLException
This constructor sets the port to -1 so the default port for the protocol will be used. The
file
argument should begin with a slash and include a path, a filename, and optionally
a fragment identifier. Forgetting the initial slash is a common mistake, and one that is
not easy to spot. Like all
URL
constructors, it can throw a
MalformedURLException
. For
example:
try
{
URL
u
=
new
URL
(
"http"
,
"www.eff.org"
,
"/blueribbon.html#intro"
);
}
catch
(
MalformedURLException
ex
)
{