I want to load a html file located in:
to use in standalone spring boot jar application.
This approach leads to FileSystemNotFoundException
new InputStreamReader(
getClass().getResourceAsStream("/email-templates/html-email.html")
)
Using
#Autowired
private ResourceLoader resourceLoader;
...
resourceLoader.getResource("classpath:email-templates/html-email.html");
leads to NullPointerException.
Could you please specify how to properly load a file in spring boot jar.
Try below code. Using spring classpathresource you should be able to load them. As long as you try load this file inside spring context, Spring container is aware of its classpath .So it should load corresponding file.Otherwise , it tries to find in file system.
Resource resource = new ClassPathResource("email-templates/html-email.html");
I assume you have these resouces in your jar, Check inside the jar file if those resources are present.
jar tf springboot.jar
Load resource in a jar file Java Spring
Try through below code.
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("email-templates/html-email.html");
/email-templates/html-email.html
If the above doesn't work visit this help full link
Java ClassLoaderUtil
Related
I have a class using ResourceLoader for loading file in my src/main/resources directory.
ResourceLoader resourceLoader = new DefaultResourceLoader(MyClass.class.getClassLoader());
Resource resource = resourceLoader.getResource(path);
File file = resource.getFile();
return new String(Files.readAllBytes(file.toPath()));
This works fine when running the app but I also have a #SpringBootTest integration test using the same code. I would like to load the same resource from main/resources but it never finds the file on the provided path. It only works for files places in src/test/resources in that context.
Does anybody know how to force the integration test to search for resources in main resource directory instead of the test one when using ResourceLoader?
I need to get the path of a key file that i have placed in the root folder of my spring application. Everything works as expected when i run it locally. But when i deploy the application to the server i get a FileNotFoundException.
File file = new File("testfile.key");
String path = file.getAbsolutePath();
I have tried placing the file in the resource folder as well.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource("testfile.key").getFile());
Just need to pass the file path to another method (3rd party library) which will read the content.
Any help would be much appreciated.
This should be a resource, placed in the resource folder (if using maven).
You can access it using
this.getClass().getResource("testfile.key");
The root for accessing files can change between environments but the root for resources is a directory that's specified during compilation. For maven driven projects this is:
src/main/resources
I am developping Spring Java application with Maven. I need to read a .json file, that is situated in Resources folder in my project, and than return it by Controller again as a .json file. How can I do that? I am a beginner...
Resourece folder is in your class path. Any file which is in your class path , you can read through this.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourFile.json");
you can read by using getResourceAsStream.
If you access to file in Spring bean you could also use #Value annotation like that (It work only in spring beans)
#Value("classpath:data/resource-data.txt")
Resource resourceFile;
https://www.baeldung.com/spring-classpath-file-access#2-using-value
I have a Spring Boot App. I am trying to read few files that I have placed under main/resources folder. I see that Spring Boot automatically reads application.properties under resources folder. However it doesn't seem to read other xml / text files that I have placed under resources folder.
Source xslt = new StreamSource(new File("removeNs.xslt"));
Should I add any additional configuration for the program to automatically read all the files under resources folder without having to explicitly provide the path?
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("removeNs.xslt").getFile());
This should work.
Thanks #Edwin for mentioning a more robust solution:
File file = new File(Thread.currentThread().getContextClassLoader().getResource("removeNs.xslt").getFile());
You need to specify that you want to read specific files under resources folder using
#PropertySource annotation.
you can specify multiple property sources using
#PropertySources({
#PropertySource(),
#PropertySource()
})
Is there a way to load files stored inside JARs using getResourceAsStream from Tomcat applications?
I have a library that puts all the files it needs inside its jar, along with the compiled classes. This code works when the library is used in standalone applications but not when the library is used inside Tomcat (using the PHP java-bridge).
final InputStream stream = Object.class.getResourceAsStream("/styles/foo.xsl");
I tried without success to use the solution outlined in question getResourceAsStream not loading resource in webapp and changed the code to
final ClassLoader resourceLoader = Thread.currentThread().getContextClassLoader();
final InputStream stream = resourceLoader.getResourceAsStream("/styles/foo.xsl");
The latter code does not work neither when the library is used standalone or when the library is used in Tomcat. In both cases stream == null.
The file I am trying to load is correctly stored on the JAR in /styles/foo.xsl. The JAR with all the classes and these other files is tomcat/webapps/iJavaBridge/WEB-INF/lib/.
Can someone suggest a piece of code that works both in Tomcat and non-Tomcat applications?
You need to remove the leading slash from the path. That would only work with classloaders which do not operate on the classpath root.
final ClassLoader resourceLoader = Thread.currentThread().getContextClassLoader();
final InputStream stream = resourceLoader.getResourceAsStream("styles/foo.xsl");
if you got a class file in the jar with the xsl try the following:
final ClassLoader resourceLoader = com.mypackage.MyClassInJar.class.getClassloader();
final InputStream stream = resourceLoader.getResourceAsStream("/styles/foo.xsl");
if there is no class, just create a dummy class.
i think that should work because you will always get the classloader responsible for the jar.