This content has been marked as final. Show 3 replies
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.
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.