Skip to content

Reading file exception #7

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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