我正在使用 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 中表現不同?
uj5u.com熱心網友回復:
將檔案移動到資源/組織/檔案中。
.txt 檔案不會按照 Maven 約定從源根目錄復制到類路徑。
如果要從源根目錄復制這些檔案,則需要調整 pom.xml 配置:https ://stackoverflow.com/a/23289401/104891 。
Eclipse 不遵循約定,您將在命令列 Maven 和 IDE 中得到不同的結果,這很糟糕。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/427790.html
標籤:智能理念
