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

Java Web项目java.io.FileNotFoundException问题排查与代码修改

Fixing FileNotFoundException for JSON Resources in Java Web Projects

Hey there, let's break down why you're seeing that FileNotFoundException and get your JSON file loaded correctly.

Why This Happens

When you're working locally, using a relative file path like ....\resources\properties\highcharts_startYearWeek.json might work because you're pointing to a physical file on your disk. But once you package your Java Web project into a WAR/JAR, the files under src/main/resources get bundled into the archive—they're no longer standalone files on the filesystem. The File class can't access resources inside the archive, which is why you're hitting the error.

Correct Ways to Load the JSON File

You need to load the resource as an input stream using classpath-based methods instead of direct file paths. Here are the most common solutions:

1. Using Class.getResourceAsStream (Universal Approach)

This works in any Java class (servlets, service classes, etc.):

// Use a classpath-relative path (leading slash means root of classpath)
try (InputStream inputStream = YourCurrentClass.class.getResourceAsStream("/properties/highcharts_startYearWeek.json")) {
    if (inputStream == null) {
        throw new FileNotFoundException("Could not find highcharts_startYearWeek.json in classpath: properties/highcharts_startYearWeek.json");
    }
    // Convert stream to JSON (example with Jackson)
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonData = objectMapper.readTree(inputStream);
    // Or map to your custom entity class
    // YourCustomEntity entity = objectMapper.readValue(inputStream, YourCustomEntity.class);
} catch (IOException e) {
    e.printStackTrace();
    // Handle exception appropriately (log, notify, etc.)
}
  • Note: The path /properties/highcharts_startYearWeek.json corresponds to src/main/resources/properties/highcharts_startYearWeek.json in your project structure.
  • The try-with-resources block ensures the stream is closed automatically.

2. Using Context ClassLoader

If you're in a context where the class's classloader might not have access (e.g., in a thread pool), use the context classloader:

try (InputStream inputStream = Thread.currentThread().getContextClassLoader()
        .getResourceAsStream("properties/highcharts_startYearWeek.json")) {
    // Same processing as above
} catch (IOException e) {
    // Exception handling
}
  • No leading slash here—this path starts directly from the classpath root.

3. Spring/Spring MVC Specific

If you're using Spring, you can inject a ResourceLoader for cleaner code:

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class YourJsonLoader {

    @Autowired
    private ResourceLoader resourceLoader;

    public void loadJson() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:properties/highcharts_startYearWeek.json");
        try (InputStream inputStream = resource.getInputStream()) {
            // Process the stream
        }
    }
}

4. Servlet-Specific Approach

If you're working directly in a servlet, use the ServletContext to access the resource:

// Inside your servlet class
try (InputStream inputStream = getServletContext()
        .getResourceAsStream("/WEB-INF/classes/properties/highcharts_startYearWeek.json")) {
    // Process the stream
} catch (IOException e) {
    // Exception handling
}
  • After packaging, src/main/resources contents end up in WEB-INF/classes in the WAR file, so this path points directly to the bundled resource.

Key Checks to Avoid Mistakes

  • Double-check the path: Ensure the path matches the structure under src/main/resources (case-sensitive on most systems!).
  • Verify the resource is bundled: After building your project, open the WAR/JAR file and confirm properties/highcharts_startYearWeek.json exists under WEB-INF/classes (for WAR) or the root of the JAR.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:03:21