Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading file exception #7

Open
Ziegeraze opened this issue Sep 9, 2023 · 0 comments
Open

Reading file exception #7

Ziegeraze opened this issue Sep 9, 2023 · 0 comments

Comments

@Ziegeraze
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant