Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classpath urls #1

Merged
merged 4 commits into from Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -12,8 +12,8 @@
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>

<build>
Expand Down
@@ -0,0 +1,38 @@
package com.stackoverflow.q76848364;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.spi.URLStreamHandlerProvider;

/**
* URL stream handler for "cp:/" URLs for accessing resources in the classpath.
* Supports a leading slash in the the path so that the scheme is treated as a
* hierarchical scheme for resolving relative URL references.
*
* <p>
* Register this provider by putting the fully qualified name of this class in
* the configuration file
* META-INF/services/java.net.spi.URLStreamHandlerProvider.
*/
public class ClasspathURLStreamHandlerProvider extends URLStreamHandlerProvider {

private static final String PROTOCOL = "cp";

@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if (PROTOCOL.equals(protocol)) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
String urlPath = url.getPath();
String resourcePath = urlPath.startsWith("/") ? urlPath.substring(1) : urlPath;
return ClassLoader.getSystemClassLoader().getResource(resourcePath).openConnection();
}
};
}
return null;
}

}
@@ -0,0 +1 @@
com.stackoverflow.q76848364.ClasspathURLStreamHandlerProvider