为什么在JDK 11中getResource()方法返回null? [英] Why does the getResource() method return null in JDK 11?

查看:378
本文介绍了为什么在JDK 11中getResource()方法返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的Java程序:

public static String loadText(String file) {
    StringBuilder finalString = new StringBuilder();

    InputStream in = null;
    BufferedReader reader = null;
    InputStreamReader isr = null;

    try{
        System.out.println("Text File: " + file);

        // Version 1
        //URL url = Thread.currentThread().getClass().getResource(file); 
        //in = url.openStream();

        // Version 2
        in = Class.class.getResourceAsStream(file);

        isr = new InputStreamReader(in);
        reader = new BufferedReader(isr);
        String line;
        while((line = reader.readLine()) != null) {
            finalString.append(line).append("//\n");
        }
    }
    catch(IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }
    finally {
        try {
            if (isr != null) { isr.close(); }
            if (reader != null) { reader.close(); }
            if (in != null) { in.close(); }
            } catch (IOException e) { e.printStackTrace(); }
        }
    return finalString.toString();
}

getResourcegetResourceAsStream方法在JDK 8(java-8-openjdk-amd64)中可以正常工作,但它们始终在JDK 11中返回null.

The getResource and getResourceAsStream methods works fine in JDK 8 (java-8-openjdk-amd64) but they always return null in JDK 11.

问题:为什么?而我该如何解决呢?

Questions: Why? And how can I fix this?

  • 操作系统:Linux Mint 19 Tara x64
  • IDE:Eclipse 2018-12(4.10.0)

推荐答案

我已经在MacOS上同时使用openjdk 8和11尝试了您的应用程序,但不能同时使用它们.我认为您需要查看[1]和[2]才能理解getResourceAsStream的工作方式.

I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream works.

TLDR:

  1. 如果路径是绝对路径(即以斜线-/开头),则class.getResourceAsStream()在提供的路径中搜索

  1. If the path is absolute (i.e. starts with a slash - /), then class.getResourceAsStream() searches in the provided path

如果该路径不是绝对路径(即,不是以斜杠开头),则class.getResourceAsStream()在与包名称相对应的构造路径中搜索,其中点被斜杠替换

If the path is NOT absolute (i.e. does not start with a slash) , then class.getResourceAsStream() searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes

所以它是否起作用取决于两件事:

So whether it works or not depends on 2 things:

  1. 您的路径是否绝对?
  2. file是否与该类位于同一个程序包中?
  1. Is your path absolute or not ?
  2. Is the file located in the same package as the class or not ?

基本上在提供的示例中,如果路径不是绝对路径,它将永远无法工作,因为Class.class.getResourceAsStream()始终会将路径解析为java/lang/<file>,因此您的文件必须位于系统软件包中.因此,您必须使用<MyClass>.class.getResourceAsStream()或使用绝对路径

Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream() will always resolve the path to java/lang/<file>, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream() or alternatively use an absolute path

[1] [2] 由于Java SE 9在命名模块中的类上调用getResourceXXX只会在该模块中定位资源,因此不会像在先前版本中那样搜索类路径.因此,当您使用Class.class.getResourceAsStream()时,它将尝试在包含java.lang.Class的模块(即java.base模块)中定位资源.显然,您的资源不在该模块中,因此它返回null.

Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream() it will attempt to locate the resource in module containing java.lang.Class, which is the java.base module. Obviously your resource is not in that module, so it returns null.

您必须使Java 9+在您的模块中搜索文件,该文件很可能是未命名的模块".您可以通过将Class更改为模块中定义的任何类来实现,以使java使用正确的类加载器.

You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class to any class defined in your module in order to make java use the proper class loader.

这篇关于为什么在JDK 11中getResource()方法返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆