Skip to content

Reading file exception #7

@Ziegeraze

Description

@Ziegeraze

Reading the File works when app is executed from the IDE, but if you compile and execute:

javac Main.java
java Main

then it throws:

Exception in thread "main" java.lang.IllegalArgumentException: File: app.log not found.
	at ResourceHelper.getAbsoluteFilePath(ResourceHelper.java:11)
	at Main.main(Main.java:15)

ResourceHelper.java:

public class ResourceHelper {

    public static String getAbsoluteFilePath(String filename) {
        URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
        if (resource == null) {
            throw new IllegalArgumentException("File: " + filename + " not found.");
        }

        try {
            Path path = Paths.get(resource.toURI());
            return path.toString();
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

}

Any suggestion to solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions