Getting the inputstream from a classpath resource (XML file)
ClassLoader.getResourceAsStream()
.
As stated in the comment below, if you are in a multi-ClassLoader
environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader()
. See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.
How to read text file from classpath in Java?
With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of:
// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("SomeTextFile.txt");
// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
If those aren't working, that suggests something else is wrong.
So for example, take this code:
package dummy;
import java.io.*;
public class Test
{
public static void main(String[] args)
{
InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
System.out.println(stream != null);
stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
System.out.println(stream != null);
}
}
And this directory structure:
code
dummy
Test.class
txt
SomeTextFile.txt
And then (using the Unix path separator as I'm on a Linux box):
java -classpath code:txt dummy.Test
Results:
true
true
load a (xml) file from the classpath in a spring web app
Spring provides a helper class named ClassPathResource
So something like:
ClassPathResource cpr = new ClassPathResource("folder/file.xsd");
InputStream is = cpr.getInputStream();
Should work, hope this helps!
Link to API Doc:
http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/core/io/ClassPathResource.html
Sidenote
Also, if you're using it for testing purposes, might want to wire a bean mapped to the xsd.
Might want to look into a JAXB marshaller
http://static.springsource.org/spring-ws/site/reference/html/oxm.html#oxm-jaxb2-xsd
Spring Boot access static resources missing scr/main/resources
Just use Spring type ClassPathResource.
File file = new ClassPathResource("countries.xml").getFile();
As long as this file is somewhere on classpath Spring will find it. This can be src/main/resources
during development and testing. In production, it can be current running directory.
EDIT: This approach doesn't work if file is in fat JAR. In such case you need to use:
InputStream is = new ClassPathResource("countries.xml").getInputStream();
How do I load a file from resource folder?
Try the next:
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");
If the above doesn't work, various projects have been added the following class: ClassLoaderUtil
1 (code here).2
Here are some examples of how that class is used:
src\main\java\com\company\test\YourCallingClass.java
src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java
src\main\resources\test.csv
// java.net.URL
URL url = ClassLoaderUtil.getResource("test.csv", YourCallingClass.class);
Path path = Paths.get(url.toURI());
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// java.io.InputStream
InputStream inputStream = ClassLoaderUtil.getResourceAsStream("test.csv", YourCallingClass.class);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
for (String line; (line = reader.readLine()) != null;) {
// Process line
}
Notes
- See it in The Wayback Machine.
- Also in GitHub.
URL to load resources from the classpath in Java
Intro and basic Implementation
First up, you're going to need at least a URLStreamHandler. This will actually open the connection to a given URL. Notice that this is simply called Handler
; this allows you to specify java -Djava.protocol.handler.pkgs=org.my.protocols
and it will automatically be picked up, using the "simple" package name as the supported protocol (in this case "classpath").
Usage
new URL("classpath:org/my/package/resource.extension").openConnection();
Code
package org.my.protocols.classpath;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
/** A {@link URLStreamHandler} that handles resources on the classpath. */
public class Handler extends URLStreamHandler {
/** The classloader to find resources from. */
private final ClassLoader classLoader;
public Handler() {
this.classLoader = getClass().getClassLoader();
}
public Handler(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
protected URLConnection openConnection(URL u) throws IOException {
final URL resourceUrl = classLoader.getResource(u.getPath());
return resourceUrl.openConnection();
}
}
Launch issues
If you're anything like me, you don't want to rely on a property being set in the launch to get you somewhere (in my case, I like to keep my options open like Java WebStart - which is why I need all this).Workarounds/Enhancements
Manual code Handler specification
If you control the code, you can do
new URL(null, "classpath:some/package/resource.extension", new org.my.protocols.classpath.Handler(ClassLoader.getSystemClassLoader()))
and this will use your handler to open the connection.
But again, this is less than satisfactory, as you don't need a URL to do this - you want to do this because some lib you can't (or don't want to) control wants urls...
JVM Handler registration
The ultimate option is to register a URLStreamHandlerFactory
that will handle all urls across the jvm:
package my.org.url;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.Map;
class ConfigurableStreamHandlerFactory implements URLStreamHandlerFactory {
private final Map<String, URLStreamHandler> protocolHandlers;
public ConfigurableStreamHandlerFactory(String protocol, URLStreamHandler urlHandler) {
protocolHandlers = new HashMap<String, URLStreamHandler>();
addHandler(protocol, urlHandler);
}
public void addHandler(String protocol, URLStreamHandler urlHandler) {
protocolHandlers.put(protocol, urlHandler);
}
public URLStreamHandler createURLStreamHandler(String protocol) {
return protocolHandlers.get(protocol);
}
}
To register the handler, call URL.setURLStreamHandlerFactory()
with your configured factory. Then do new URL("classpath:org/my/package/resource.extension")
like the first example and away you go.
JVM Handler Registration Issue
Note that this method may only be called once per JVM, and note well that Tomcat will use this method to register a JNDI handler (AFAIK). Try Jetty (I will be); at worst, you can use the method first and then it has to work around you!
License
I release this to the public domain, and ask that if you wish to modify that you start a OSS project somewhere and comment here with the details. A better implementation would be to have a URLStreamHandlerFactory
that uses ThreadLocal
s to store URLStreamHandler
s for each Thread.currentThread().getContextClassLoader()
. I'll even give you my modifications and test classes.
Related Topics
Singleton Design Pattern: Pitfalls
Directly Convert CSV File to JSON File Using the Jackson Library
Differences Between "Java -Cp" and "Java -Jar"
What Are the Pros and Cons of Performing Calculations in SQL VS. in Your Application
Pageloadtimeout in Selenium Not Working
How to Avoid Type Safety Warnings with Hibernate Hql Results
Should Getters and Setters Be Synchronized
How to Use Mdc with Thread Pools
Java.Lang.Nosuchmethoderror: Javax.Servlet.Servletcontext.Getcontextpath()Ljava/Lang/String;
How to Keep the Iteration Order of a List When Using Collections.Tomap() on a Stream
How to Test That No Exception Is Thrown
Convert a Date Format in Epoch
Java Sax Parser Split Calls to Characters()
Composing Swing Components: How to Add the Ability to Add Actionlisteners
Java Swing: How to Implement a Login Screen Before Showing a Jframe