Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java URL Protocols: classpath:/?

I've seen some Spring code that read config files and other resources directly off the runtime classpath using the classpath:/some/path/to/resource URL protocol.

Is this a Spring construct or a Java construct?

I can't find any documentation besides this question - URL to load resources from the classpath in Java, which doesn't indicate either way.

If it's a Java construct, can anyone point me to its official documentation?

like image 691
IAmYourFaja Avatar asked Aug 17 '12 13:08

IAmYourFaja


2 Answers

Well you can always register URL handlers. Java also has a file:/// and jar: handler. Also class.getResource will by default read from the classpath.

http://code.google.com/p/madura-classpath-protocol-handler/

apparently it is a spring feature.

"You can see these standard handlers, and associated implementation classes,in the JDK's RT.JAR file. Look for classes whose fully-qualified name starts with sun.net.www.protocol.For example,the class sun.net.www.protocol.http.Handler defines the HTTP protocol handler. Class sun.net.www.protocol.ftp.Handler defines the FTP protocol handler class."

http://java.sun.com/developer/onlineTraining/protocolhandlers/

Trying to use classpath: in Java 1.6 results in:

Exception in thread "main" java.net.MalformedURLException: unknown protocol: classpath

like image 63
Markus Mikkolainen Avatar answered Nov 12 '22 11:11

Markus Mikkolainen


classpath: is specific to spring. Spring's resource resolving mechanism (ie. PathMatchingResourcePatternResolver or other imlementations) knows about the "classpath:" and "classpath*:" prefixes.

It takes that and resolves into ClassPathResource object(s), which happen to implement springs Resource interface.

The Resource interface, among other things, has a getInputStream() method which can be used to get the contents, without having to be aware of what type of resource it is.

This is entirely separate from any URL protocol handling, so you won't necessarily be able to directly add this as a protocol handler.

However, you may be able to make some use of the ClassPathResource class itself as part of your protocol handler.

like image 38
Matt Avatar answered Nov 12 '22 11:11

Matt