getClass().getResource(resourcePath) valid on windows, null on Linux
Asked Answered
M

3

7

I have a problem, this call

URL fileURL = getClass().getResource(resourcePath);

works on Windows (7 64b) but not on linux (Ubuntu 13.10 64b) where it returns null.

Why? File is there and the string is the following (relative path)

String resourcePath = "/tut01/shaders/vertex_shader.glsl"

Both file are in my home

Edit: The project was freshly cloned and I forgot to clean & build, sorry for that.. So now it founds them. However it is strange because even if I modify, let's say, the vertex_shader.glsl, my program will refer always to the old version, every time I edit it, I need to do clean & build in order to see the changes... Why? On windows I don't have to do that..

Mimimimic answered 9/2, 2014 at 10:44 Comment(13)
It depends what you pass in for resourcePath, I guess.Carrelli
Yeah, I was adding it when you were writing your comment :)Mimimimic
What is the exact path to the file? (On Windows and Linux)Elisabetta
@immibis on Linux /home/elect/Documents/modern-jogl-examples/modern-jogl-examples/src/tut01/shaders/fragment_shader.glsl on win I will tell you tomorrow since I do not have access right nowMimimimic
Are you using an IDE? If yes, which one? If not, is /home/elect/Documents/modern-jogl-examples/modern-jogl-examples/src/ on the classpath?Elisabetta
Yep, I am using NetbeansMimimimic
Does Netbeans copy them to somewhere like /home/elect/Documents/modern-jogl-examples/modern-jogl-examples/*bin*/tut01/shaders/fragment_shader.glsl? If not, try doing that manually and see if that works. I don't know how Netbeans handles resources.Elisabetta
It doesn't seem so. elect@elect-desktop:~/netbeans-7.4/bin$ sudo find / -name fragment_shader.glsl [sudo] password for elect: /home/elect/Documents/modern-jogl-examples/modern-jogl-examples/src/tut01/shaders/fragment_shader.glsl What do you mean? Just trying to see what happens if I move them in another location?Mimimimic
Java will look for the resource in the same place where the class files are. If your class files are at .../modern-jogl-examples/bin/<package>/<filename>.class, then it will look for resources in .../modern-jogl-examples/bin/<the string you pass to getResource>. If Netbeans isn't automatically copying them to the correct location, then something is probably wrong with the way Netbeans is set up.Elisabetta
I had a similar problem. Key was that my filename wasn't exactly the same so that the case-sensitive linux couldnt find the file. e.g: "MyFile.xlsx" vs "Myfile.xlsx"Graceless
It sounds that you use Maven. It by default copies resources to target directory during building. When you run your program (without re-building), it reads not-edited copy from target, not original file from sources. You should edit copy file in order to see immediate effects, or re-build. Maven's "clean" phase removes target directory, Maven's "build" phase recreates target directory, so it copies original resource files again.Landel
path in Intellij?Autoeroticism
Sorry but I dont rememberMimimimic
B
1

Your resource path starts with a / and is therefore an absolute path. If you want the resource path to be relative you have to omit the first /.

From the Javadoc of Class.getResource(String name):

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form: modified_package_name/name where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

A relative path is relative to the path of the class returned by getClass().

An example:

package org.example;

public class MyClass {
    public void foo() {
        getClass().getResource("tut01/shaders/vertex_shader.glsl");
    }
}

Let's assume the compiler writes the compiled class file to /home/my-project/bin/org/example/MyClass.class.

getClass().getResource("tut01/shaders/vertex_shader.glsl") would then look for the file in /home/my-project/bin/org/example/tut01/shaders/vertex_shader.glsl.

Bend answered 9/2, 2014 at 11:8 Comment(0)
D
0

Seems you donot have read access to resourcePath location Try putting value in resourcePath which you have access to i.e. you should be able to see the file

Downturn answered 9/2, 2014 at 10:50 Comment(0)
C
0

Check if the Linux account running your Java program has all the necessary
permissions (for the file and the folders on the file path). If the file is indeed
there, then permissions could be the issue.

Carrelli answered 9/2, 2014 at 10:52 Comment(3)
Tried to run Netbeans with sudo, but no success.. Is it legit to believe that running Netbeans with sudo automatically will run also the java application with the corresponding privileges?Mimimimic
Well, it would not be NetBeans, it is the Java program you created in it. I don't think you can assume that unless you read it on some official documentation page. I would compile the program and run it outside of NetBeans. If your program is too complex, write some small program to help you isolate/detect if it's indeed a permissions issue or not.Carrelli
However everything, the project and data, is located within my home folder, it should have the necessary privileges... However I will try to do as you say. Do you know how can I access resources outside the IDE?Mimimimic

© 2022 - 2024 — McMap. All rights reserved.