You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java测试代码读取项目文件触发NoSuchFileException问题求助

Fixing java.nio.file.NoSuchFileException for Your Test Resource File

Let’s work through why your code suddenly can’t locate that config file and get it back up and running. Here are the most common fixes and checks to try:

1. Verify Your Working Directory

Relative paths like src/test/resources/... rely entirely on the working directory where your JVM is executing. If this changed (e.g., you adjusted your IDE’s run configuration, or you’re running the code from a different folder in the terminal), the path won’t resolve correctly.

Add this quick line to print your current working directory and confirm it’s your project root:

System.out.println("Current working directory: " + System.getProperty("user.dir"));

If the output isn’t your project’s root folder, either adjust your file path to be relative to this directory, or update your runtime settings to set the working directory back to your project root.

2. Double-Check the File’s Existence & Path Spelling

  • Manually navigate to src/test/resources in your project—did someone delete, move, or rename SenegalAndBulgariaConfig.txt?
  • Case sensitivity matters on Unix/Linux/macOS. If the file is named senegalAndBulgariaConfig.txt (lowercase 's'), your path won’t find it.
  • Also, your second line of code (Paths.get("src/")) tries to read a directory with readAllBytes()—this will always throw an exception, since readAllBytes() only works on files. If you need to interact with the directory, use Files.list() instead.

3. Use Classpath Resource Loading (More Reliable!)

For test resources in src/test/resources, the best practice is to load them via the classpath instead of relative file paths. This avoids working directory issues entirely, since the resource gets copied to your test classpath during the build process.

Try this approach instead:

// Using your test class's class loader
try (InputStream inputStream = getClass().getResourceAsStream("/SenegalAndBulgariaConfig.txt")) {
    if (inputStream == null) {
        throw new FileNotFoundException("Resource not found in classpath");
    }
    String fileContents = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
    // Use your file contents here
} catch (IOException e) {
    e.printStackTrace();
}

If you’re working in a static method, use the context class loader:

try (InputStream inputStream = Thread.currentThread().getContextClassLoader()
        .getResourceAsStream("SenegalAndBulgariaConfig.txt")) {
    // Same read logic as above
}

4. Check Your Build Tool’s Resource Handling

If you’re using Maven or Gradle:

  • Confirm src/test/resources is marked as the test resource directory (this is the default, but custom builds can override this).
  • After running a test build, check the target/test-classes (Maven) or build/resources/test (Gradle) folder—your SenegalAndBulgariaConfig.txt should be present there. If it’s missing, review your build config for rules that exclude resources.

Quick Summary

  • Replace relative file paths with classpath loading for test resources—it’s far more reliable across different runtime environments.
  • Always verify your working directory when relative paths fail unexpectedly.
  • Fix that second line trying to read a directory with readAllBytes()—it’s invalid for directory paths.

内容的提问来源于stack exchange,提问作者Elad Benda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:26:04