[JAVA] The behavior of Class # getClassLoader changes depending on whether it is executed in the IDE or jar.

Overview

When I run it in a jar, I can't find the resource when two slashes like the one below overlap.

            InputStream resource1 = this.getClass().getClassLoader().getResourceAsStream("test//hello.txt");

Project structure

スクリーンショット 2018-03-22 9.18.52.png

Sample code

package work.inabajun;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        new Test().getHelloText();
    }

    private static class Test {

        public void getHelloText() throws IOException {
            //One slash
            InputStream resource1 = this.getClass().getClassLoader().getResourceAsStream("test/hello.txt");
            if( resource1 != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource1));
                System.out.println("Resource1:" + bufferedReader.readLine());
            } else {
                System.out.println("Resource1 is null.");
            }

            //Two slashes
            InputStream resource2 = this.getClass().getClassLoader().getResourceAsStream("test//hello.txt");
            if( resource2 != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource2));
                System.out.println("Resource2:" + bufferedReader.readLine());
            } else {
                System.out.println("Resource2 is null.");
            }
        }
    }
}

Start-up

Launch in Intellij IDEA

$ java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=60858:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8  -classpath /Users/inabajunmr/test/untitled/out/production/untitled work.inabajun.Main
objc[7462]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x1023c94c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x103bf04e0). One of the two will be used. Which one is undefined.
Resource1:abcde12345
Resource2:abcde12345

Both Resource1 and Resource2 can be acquired.

Create and launch an executable jar

$ java -jar untitled_jar/untitled.jar 
Resource1:abcde12345
Resource2 is null.

Resource2 becomes null.

I was told that the template could not be found in Spring Boot + Thymeleaf

When I wrote the following code with Spring Boot + Thymeleaf, it worked normally when booting with Gradle or IDE

	@GetMapping("/list.html")
	public String viewInput(final Model model) {
		return "/resource/list";
	}

When I deployed it to the environment, I was angry that there was no template.

2018-03-21 11:58:56.840 ERROR 26381 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/resource/list", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/resource/list", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354)

The difference in this behavior. https://github.com/spring-projects/spring-boot/issues/1744

Code that worked properly

After removing the leading slash, it worked with either startup method.

	@GetMapping("/list.html")
	public String viewInput(final Model model) {
		return "resource/list";
	}

Recommended Posts

The behavior of Class # getClassLoader changes depending on whether it is executed in the IDE or jar.
Behavior when each is executed in the reverse order Range
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
Change the conversation depending on which day of the week is today
Different buffering and flushing rules depending on whether the standard output is a console or another command
Error when the member of Entity class used in SpringWebFlux is final
[Java] Where is the implementation class of annotation that exists in Bean Validation?
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Retrieve the first day of week in current locale (what day of the week is it today?)
Is it mainstream not to write the closing tag of <P> tag in Javadoc?