【问题标题】:Intellij - Not able to read a file from other package in the same moduleIntellij - 无法从同一模块中的其他包中读取文件
【发布时间】:2022-02-06 18:48:33
【问题描述】:

我正在使用 Intellij Idea - 2021.3.2 和 Java 8。在 Intellij 中,我无法读取同一模块中另一个包中的文件。该文件位于主文件夹而不是资源文件夹下。但在 Eclipse 中,我可以阅读。

例如。下面的代码在 eclipse 和 intellij 中打印不同的输出。

    import java.net.URL;

public class TestMain {
    public static void main(String[] args) {
        URL resource = TestMain.class.getResource("/org/files/input.txt");
        if(resource == null) {
            System.out.println("Resource is null");
        } else {
            System.out.println("Resource found!!");
        }
    }
}

在 Intellij 中,代码打印“Resource is null”,但在 eclipse 中,代码打印“Resource found!!”。

我需要在 intellij 中启用/禁用任何设置吗?为什么 Intellij Idea 中的代码表现不同?

【问题讨论】:

    标签: intellij-idea


    【解决方案1】:

    将文件移动到资源/组织/文件中。

    .txt 文件不会按照 Maven 约定从源根目录复制到类路径。

    如果要从源根目录复制这些文件,需要调整 pom.xml 配置:https://stackoverflow.com/a/23289401/104891

    Eclipse 没有遵循约定,你会在命令行 Maven 和 IDE 中得到不同的结果,这很糟糕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 2016-05-29
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多