Eclipse中F11运行报FileNotFoundException,Ctrl+F11正常求助
Hey there! Let's break down why your Java program works with Ctrl+F11 (normal run) but throws FileNotFoundException when using F11 (debug mode):
Root Cause
The key difference here is the working directory Eclipse uses for normal runs vs debug runs. When you run normally, Eclipse sets the working directory to your project root folder, so the relative path src/mainP/HistogramText.txt points to the right file. But in debug mode, sometimes the working directory gets changed (e.g., to the bin or another subfolder), making that relative path invalid.
Plus, since your file is inside the mainP package, treating it as a classpath resource is more reliable than using a filesystem path—it works regardless of the working directory.
Solution 1: Use Classpath Resource (Recommended)
Instead of relying on filesystem paths, load the file using your class loader. This way, Java will look for the file in the compiled classpath (where your package files live after compilation):
Modify your Temp() method like this:
private int[] Temp() throws IOException { int num = 0; int[] arr = new int[12]; // Use class loader to get the resource stream from the package try (Scanner fScan = new Scanner(getClass().getResourceAsStream("HistogramText.txt"))) { while (fScan.hasNext()) { num = fScan.nextInt(); arr[11]++; if (num > 100) { arr[10]++; } if (num >= 1 && num <= 10) { arr[0]++; } if (num >= 11 && num <= 20) { arr[1]++; } if (num >= 21 && num <= 30) { arr[2]++; } if (num >= 31 && num <= 40) { arr[3]++; } if (num >= 41 && num <= 50) { arr[4]++; } if (num >= 51 && num <= 60) { arr[5]++; } if (num >= 61 && num <= 70) { arr[6]++; } if (num >= 71 && num <= 80) { arr[7]++; } if (num >= 81 && num <= 90) { arr[8]++; } if (num >= 91 && num <= 100) { arr[9]++; } } } catch (IOException e) { e.printStackTrace(); } return arr; }
Notes:
getClass().getResourceAsStream("HistogramText.txt")looks for the file directly in the same package as yourHistogramBarChartclass (which matches where you placed it).- We use a try-with-resources block (
try (Scanner fScan = ...)) to automatically close the scanner, which is better practice for resource management. - Update the method signature to throw
IOException(or handle it properly inside the catch block).
Solution 2: Fix Eclipse Debug Working Directory
If you want to stick with the filesystem path, adjust the debug configuration to use the project root as the working directory:
- Go to Run > Debug Configurations
- Select your Java Application configuration (the one you use to run your program)
- Switch to the Arguments tab
- Look for the Working directory section
- Select Other and click Workspace...
- Choose your project folder and click OK
- Apply the changes and debug again
Quick Pre-Check
Make sure your HistogramText.txt file is set to be copied to the classpath during compilation:
- Right-click the file in Eclipse > Properties
- Under Resource > Resource Filters, ensure there's no filter excluding it
- Under Java Build Path > Source, check that your
srcfolder is marked as a source folder, and that the file is included in the build output
This should resolve the FileNotFoundException in both run and debug modes!
内容的提问来源于stack exchange,提问作者Sevajper

