Java getResource is in wrong path
Asked Answered
H

3

1

I hope someone can help me here, becouse I'm fighting with a problem for some time. In my main class I use this command:

System.out.println(getClass().getClassLoader().getResource("org"));

The problem I've got is that it returns:

file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main/org

instead of:

file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main/org

The problem is that it goes into classes directory instead of resources dir. As a result I can't have access to my .fxml files I need. I'm using gradle for build and currently working with JavaFX. I've tried something like:

System.out.println(getClass().getClassLoader().getResource("/resources/java/main/org"));

But I just got null :(

Do you know any method to force him to use absolute path or to look for resources in resource filder or even use something like to use "../" from linux to go up. I dodn;t find any of this

Haeres answered 3/12, 2019 at 12:16 Comment(3)
You can't list the content of packages with resources anyway. getResource() should be used with the path of a resource, i.e. a file, not a directory.Upthrow
Your resources directory is essentially a source tree. Programs do not have access to their source files at runtime. You are supposed to copy resources to the same place where compiled class files reside.Vasileior
The issue for IntelliJ users is at youtrack.jetbrains.com/issue/IDEA-119280Meathead
M
1

The root of your resources tree is defined by the classloader (as described in the JavaDoc). You can define the root by explicitely setting it in your classpath or preferably by using a build tool like maven and following the conventions set and used by the tool. For maven projects the root would usually be at main/java/resources.

Maurene answered 3/12, 2019 at 12:23 Comment(0)
C
1

getResource will always return the first match in the class path. So if you specify E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main before E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main in your classpath, you will get what you want.

That said, the resources are usually meant to be copied with the classes, and sometimes both are packed in a jar file, so you shouldn't worry about it.

Cordey answered 3/12, 2019 at 12:27 Comment(1)
Gradle does it right, but not IntelliJ. Do you have any hint how to do that in IntelliJ?Meathead
M
-1

With JavaFX use FXMLLoader;

     FXMLLoader.load(new URL(getClass().getResource("/fxml/myfxml.fxml").toExternalForm()));

Make sure to pass the platform appropriate separator and use a relative path.

Marsh answered 3/12, 2019 at 12:58 Comment(3)
This is mostly true (except that you always use a forward-slash in an argument to getResource, because the argument is a relative URL, not a file path), but it does not answer the question.Vasileior
In what way does not not answer the question this will load the FXML from the resources directory.Marsh
The question is asking why the getResource method does not find the resource files located in the project’s resources directory.Vasileior

© 2022 - 2024 — McMap. All rights reserved.