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..

elect
  • 6,765
  • 10
  • 53
  • 119
  • It depends what you pass in for `resourcePath`, I guess. – peter.petrov Feb 09 '14 at 10:45
  • Yeah, I was adding it when you were writing your comment :) – elect Feb 09 '14 at 10:46
  • What is the exact path to the file? (On Windows and Linux) – user253751 Feb 09 '14 at 10:52
  • @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 now – elect Feb 09 '14 at 11:20
  • 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? – user253751 Feb 09 '14 at 11:21
  • Yep, I am using Netbeans – elect Feb 09 '14 at 11:21
  • 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. – user253751 Feb 09 '14 at 11:23
  • 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? – elect Feb 09 '14 at 11:33
  • 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//.class, then it will look for resources in .../modern-jogl-examples/bin/. If Netbeans isn't automatically copying them to the correct location, then something is probably wrong with the way Netbeans is set up. – user253751 Feb 09 '14 at 11:40
  • 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" – Jérôme Oct 04 '18 at 10:00
  • 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. – sgnsajgon Apr 06 '19 at 05:03
  • path in Intellij? – CodeSlave Feb 26 '20 at 21:17
  • Sorry but I dont remember – elect Feb 27 '20 at 08:47

3 Answers3

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.

eclipse-pmd
  • 545
  • 2
  • 12
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

user3289405
  • 127
  • 1
  • 9
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.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • 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? – elect Feb 09 '14 at 10:56
  • 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. – peter.petrov Feb 09 '14 at 10:57
  • 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? – elect Feb 09 '14 at 11:06