This discussion is archived
3 Replies Latest reply: Dec 29, 2012 10:58 AM by jsmith RSS

no looking other folders

anersantana Newbie
Currently Being Moderated
I post it here cuz only fx is causing this. If file is on other folder, netbeans is not seeing. Example:

scene.getStylesheets().add(MainLogin.class.getResource("Login.css").toExternalForm()); workis perfeclty if I have the css or whatever on the same folder, but

scene.getStylesheets().add(MainLogin.class.getResource("../css/Login.css").toExternalForm()); doesnt work. if I set the css or whatever on the the folder does not work.
has anyone experience that problems?

Edited by: 976966 on 28-dic-2012 18:45
  • 1. Re: no looking other folders
    jsmith Guru
    Currently Being Moderated
    The jar: protocol does not understand the concept of a .. path in a directory within a jar.

    When you call getResource, it uses the protocol of the class loader used to load the class you are calling getResource from.
    If you loaded the class from the file system using the file: protocol, then a relative url like .. will work.
    If you loaded the class from a jar archive using the jar: protocol, then a relative url like .. will not work.

    This is not a JavaFX specific thing though it might seem like it.

    It appears that JavaFX is causing the issue because a NetBeans JavaFX project is packaged into a jar and executed from the jar archive, thus the class loader uses the jar: protocol.

    Standard Java projects in NetBeans do not use the JavaFX packaging tools to create a jar and just run the application from the class files in the filesystem, thus the class loader uses the file: protocol.

    Instead of trying to get a resource with a .. in it's path, use an absolute address or an address which is relative via a subdirectory to the path to the class you are getting the resource from.
    For example, the following will all work:
    MainLogin.class.getResource("Login.css").toExternalForm()      // fetch Login.css from the same directory as the MainLogin class.
    MainLogin.class.getResource("css/Login.css").toExternalForm()  // fetch Login.css from the css subdirectory of the directory holding the MainLogin class.
    MainLogin.class.getResource("/Login.css").toExternalForm()     // fetch Login.css from the root directory or the jar containing the MainLogin class.
    MainLogin.class.getResource("/css/Login.css").toExternalForm() // fetch Login.css from the css subdirectory of the root directory or the jar containing the MainLogin class.
  • 2. Re: no looking other folders
    anersantana Newbie
    Currently Being Moderated
    Thanks... works like a charm. Just one thing. I have netbeans 7.2, and one partner has netbeans 7.1 and he use .. and works with him. But now I see that without the .. works perfectly
  • 3. Re: no looking other folders
    jsmith Guru
    Currently Being Moderated
    I have netbeans 7.2, and one partner has netbeans 7.1 and he use .. and works with him.
    The JavaFX project management in NetBeans changed between 7.1 and 7.2. With 7.1 it did not perform the packaging before you ran the project in NetBeans, so it just ran the project off the file system, where relative resource specifications like .. work. With 7.2 it always, the run command always packages as a jar before executing the project, so relative resource specifications like .. don't work. The 7.2 behaviour is "more correct", because when you deploy your app to users you will always have it packaged in a jar, so the project with .. in it for resource fetch will always break eventually when you go to deploy it => better to catch the error at development time and fix it there.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points