Java测试代码读取项目文件触发NoSuchFileException问题求助
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/resourcesin your project—did someone delete, move, or renameSenegalAndBulgariaConfig.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 withreadAllBytes()—this will always throw an exception, sincereadAllBytes()only works on files. If you need to interact with the directory, useFiles.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/resourcesis 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) orbuild/resources/test(Gradle) folder—yourSenegalAndBulgariaConfig.txtshould 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

