LoginSignup
0
2

More than 5 years have passed since last update.

Class#getClassLoaderの挙動がIDEで実行した場合とjarを実行した場合で変わる

Last updated at Posted at 2018-03-22

概要

jarで実行すると以下のようなスラッシュが2つ重なる場合にリソースを見つけられない。

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

プロジェクト構成

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

サンプルコード

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 {
            // スラッシュが1つ
            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.");
            }

            // スラッシュが2つ
            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.");
            }
        }
    }
}

起動

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

Resource1,2共に取得できる。

実行可能jarを作成して起動

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

Resource2がnullになる。

Spring Boot + Thymeleafでテンプレートが見つからないって言われた

Spring Boot + Thymeleafで以下のようなコードを書いたらGradleもしくはIDEでの起動時には普通に動いたのに

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

環境にデプロイしたらテンプレートがありませんって言って怒られた。

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)

この挙動の差とのこと。
https://github.com/spring-projects/spring-boot/issues/1744

ちゃんと動いたコード

先頭のスラッシュを消したらいずれの起動方法でも動いた。

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2