JDK源码中的构造方法,并看一下方法的注释。
核心抽象:
Creates a URL object from the specified protocol, host, port number, and file.
支持协议:
Protocol handlers for the following protocols are guaranteed to exist on the search path :-
http, https, file, and jar
附: JDK 1.8 源码
/**
* Creates a {@code URL} object from the specified
* {@code protocol}, {@code host}, {@code port}
* number, and {@code file}.<p>
*
* {@code host} can be expressed as a host name or a literal
* IP address. If IPv6 literal address is used, it should be
* enclosed in square brackets ({@code '['} and {@code ']'}), as
* specified by <a
* href="http://www.ietf.org/rfc/rfc2732.txt">RFC 2732</a>;
* However, the literal IPv6 address format defined in <a
* href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP
* Version 6 Addressing Architecture</i></a> is also accepted.<p>
*
* Specifying a {@code port} number of {@code -1}
* indicates that the URL should use the default port for the
* protocol.<p>
*
* If this is the first URL object being created with the specified
* protocol, a <i>stream protocol handler</i> object, an instance of
* class {@code URLStreamHandler}, is created for that protocol:
* <ol>
* <li>If the application has previously set up an instance of
* {@code URLStreamHandlerFactory} as the stream handler factory,
* then the {@code createURLStreamHandler} method of that instance
* is called with the protocol string as an argument to create the
* stream protocol handler.
* <li>If no {@code URLStreamHandlerFactory} has yet been set up,
* or if the factory's {@code createURLStreamHandler} method
* returns {@code null}, then the constructor finds the
* value of the system property:
* <blockquote><pre>
* java.protocol.handler.pkgs
* </pre></blockquote>
* If the value of that system property is not {@code null},
* it is interpreted as a list of packages separated by a vertical
* slash character '{@code |}'. The constructor tries to load
* the class named:
* <blockquote><pre>
* <<i>package</i>>.<<i>protocol</i>>.Handler
* </pre></blockquote>
* where <<i>package</i>> is replaced by the name of the package
* and <<i>protocol</i>> 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 {@code URLStreamHandler}, then the next package
* in the list is tried.
* <li>If the previous step fails to find a protocol handler, then the
* constructor tries to load from a system default package.
* <blockquote><pre>
* <<i>system default package</i>>.<<i>protocol</i>>.Handler
* </pre></blockquote>
* If this class does not exist, or if the class exists but it is not a
* subclass of {@code URLStreamHandler}, then a
* {@code MalformedURLException} is thrown.
* </ol>
*
* <p>Protocol handlers for the following protocols are guaranteed
* to exist on the search path :-
* <blockquote><pre>
* http, https, file, and jar
* </pre></blockquote>
* Protocol handlers for additional protocols may also be
* available.
*
* <p>No validation of the inputs is performed by this constructor.
*
* @param protocol the name of the protocol to use.
* @param host the name of the host.
* @param port the port number on the host.
* @param file the file on the host
* @exception MalformedURLException if an unknown protocol is specified.
* @see java.lang.System#getProperty(java.lang.String)
* @see java.net.URL#setURLStreamHandlerFactory(
* java.net.URLStreamHandlerFactory)
* @see java.net.URLStreamHandler
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler(
* java.lang.String)
*/
public URL(String protocol, String host, String file)
throws MalformedURLException {
this(protocol, host, -1, file);
}
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
{
this(protocol, host, port, file, null);
}
public URL(String protocol, String host, int port, String file,
URLStreamHandler handler) throws MalformedURLException {
if (handler != null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// check for permission to specify a handler
checkSpecifyHandler(sm);
}
}
protocol = protocol.toLowerCase();
this.protocol = protocol;
if (host != null) {
/**
* if host is a literal IPv6 address,
* we will make it conform to RFC 2732
*/
if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
host = "["+host+"]";
}
this.host = host;
if (port < -1) {
throw new MalformedURLException("Invalid port number :" +
port);
}
this.port = port;
authority = (port == -1) ? host : host + ":" + port;
}
Parts parts = new Parts(file);
path = parts.getPath();
query = parts.getQuery();
if (query != null) {
this.file = path + "?" + query;
} else {
this.file = path;
}
ref = parts.getRef();
// Note: we don't do validation of the URL here. Too risky to change
// right now, but worth considering for future reference. -br
if (handler == null &&
(handler = getURLStreamHandler(protocol)) == null) {
throw new MalformedURLException("unknown protocol: " + protocol);
}
this.handler = handler;
}
JVM启动时,通过 Launcher启动,优先加载了URLCLassPath,其中的资源地址采用file协议的url进行初始化及加载。
启动过程中会通过URL作为资源地址,并通过com.net.www.protocol 下的协议handler加载启动类资源。