I currently have a major problem with loading of CSS and images in JavaFX.
The goal is to make JavaFX load the images that are defined in the CSS file. I get this to work easily in the IDE and in the standalone execution. But once I try the the application as a applet and run it inside a browser context everything fails.
The CSS file is still load properly, but the image files remain blank. Sadly I can't find a way to make JavaFX log why the image loading is failing. All the images are located in subdirectories from the location of the CSS file and are accessed for example like this:
.button-gray {
-fx-border-image-source: url("button/buttongray.png");
}
The CSS file is located in the same package as the class that handles loading it and is load like this:
final URL css = Util.class.getResource("sheet.css");
if (css != null) {
parent.getStylesheets().add(css.toExternalForm());
}
I tried already placing the resources in the root directory and load it with Util.class.getClassLoader.getResource(...) and Thread.currentThread().getContextClassLoader.getResource(...). Both worked fine in case the application was executed as stand alone. Neither worked in case the application is launched from a webstart applet context.
But as I said. In all cases there is no indication that the CSS is not load. The styles defined in the stylesheet are applied properly with exception of the images.
I am running out of idea what the reason for this is. I package and publish the application using the gradle javafx plugin by shemnon.
Building environment:
Oracle Java 1.7b45 x64
Gradle 1.9
Anyone know how to fix this problem or has any idea how to debug it.
Sadly the logging facilities of JavaFX (even the CSS Logger) and the applet trace console give no indication what the problem is.
New Information!
The JNLP file is located here:
JNLP-File
How ever, this file is not the problem. The problems seems to be the generation of the binary css file that is part of the deployment process of JavaFX for webstart. In this binary file, for some yet unknown reason there is a reference to the CSS file inside by building environment. This causes the CSS loader to load the image files from the location on my building server. Something that does not work in my local computer. Builds I did on my local computer on the other hand work because the files are still at the location its looking for.
So now the problem seems to be limited to the binary css generation that stores a entirely wrong file reference.
1) Can you post the .jnlp file that you're using to deploy the app? An incorrect .jnlp can cause resource loading issues like this.
2) Give us the exact invocation of Thread.currentThread().getContextCLassLoader.getResource("") that you're using.
3) Report the contents of the .jar file, with the exact folder/path structure of the file(s) in the jar that you need to load. For example, 'My code is looking for example.png, it should be in the pics.jar file inside the folder com/mycompany/myimages', something like that.
WebStart takes some doing to get working, but I'd suspect the answer lies in there somewhere. If all else fails, I've found JaNeLa to be helpful in debugging web start deployment problems. http://pscode.org/janela/
Have you tried loading the css file with:
final String css = getClass.getResource("sheet.css").toExternalForm();
parent.getStylesheets.add(css); // taken that parent is the name for the Scene.
For the css:
-fx-border-image-source: url("../button/buttongray.png");
Using URL and Util.class is not something that is common to use for loading stylesheets afaik.
Maybe try NetBeans IDE 7.4. Personally i don't know Gradle.
Related
I have a heavy monolithic guidewire application that has both fop.jar and xmlgraphics-commons.jar.
So, I am trying to add an image into a pdf file which is supposed to get rendered with the xmlgraphics-commons.jar. This jar has about 5/6 image renderers available .
Now when I try to print this pdf, the fop jar is used to render some other text onto the pdf and for the image, the xmlgraphics-commons.jar within the fop jar is used. This particular version of the same jar has somehow just 2 image decoders and although I tried to match the format SVG/WMF, it still doesn't work. I get this 'No-ImagePreloader Found' error.This gw application works correctly in my local but when deployed to websphere, it gives this error. Maybe something because of classloader policies.
I cannot use a custom class loader as I don't have control over the code here because the java call happens internally as I am using .pcf files for pdf printing.
I examined the fop and xmlgraphics-commons jars and clearly see the difference in the Meta-Inf Services files.
What can I do to get this working ? Maybe there is a simple solution I am missing out on.
i can only work with these jars.
I checked some similar questions but they dont help me. Also this below thread might get you understand the crux of my issue better.
Apache FOP in a Java Applet - No ImagePreloader found for data
I tried updating the 'org.apache.xmlgraphics.image.loader.spi.ImageLoader' file in meta-inf/services of fop to give the implementation from my external xmlgraphics jar. Got the same error.
If this application had been a maven one, I would have simply excluded this xmlgraphics within fop and used the external one.
I have written a Java WebApp with Vaadin 14.8.0 and SpringBoot.
When I put the application in production mode and create a war file with the command "mvn clean package -Pproduction" and deploy it on my Wildfly, everything works normally. My CSS files are read and also activated.
However, the path I use in the css file for the background image is not found.
"background-image: url("/META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg");"
If I enter the path directly as url:
"http://localhost:8080/planyoureplaylist-1.0-SNAPSHOT/META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg"
I also get a 404 Not Found.
My css files are located in the root directory under ./frontend/styles.
My image files can be found in src/main/resources/META-INF/resources/img/.
I also looked at my WAR file with WINRAR. Since I notice that I find the image files in the directory WEB-INF\classes\META-INF\resources\img, but in the whole folder structure not my css files.
To my surprise the background image was displayed normally when I started the application not yet via an external wildfly.
In reference to the following link https://github.com/vaadin/flow/issues/11015 I understand that it is a bug of vaadin. However, since I am on Vaadin 14.8.0 and the bug should be fixed with 14.6.2 I do not understand the problem.
Gladly point out if something else is needed to solve the problem.
About help I would be very happy.
Thanks in advance
The files in META-INF/resources are published in the server root so your META-INF/resources/img/zac-bromell-QwrTnOlWAmI-unsplash.jpg file is available at img/zac-bromell-QwrTnOlWAmI-unsplash.jpg inside your context root. Your background image CSS should thus be "background-image: url("img/zac-bromell-QwrTnOlWAmI-unsplash.jpg");"
im wring a simple program and my images load in fine in the project but as soon as i export it into a jar file none of them load. i have little to no experiance creating artifacts or jar files.
project structure:
project:
src:
myclass1, myclass2...
res:
image1.png image2.png...
im using Toolkit.getdefaultToolkit to load in images
in my class i am loading the images in the constructor by writing
myImage = toolkit.getImage("res/image1.png");
this works perfectly fine in the project. does not work in the jar
i have also tried
myImage = toolKit.getImage("image1.png");
which does not work in the project or while opening the jar
i know toolkit is not the best way to go about loading images but i would like to know how i can fix this issue while using toolkit.
ive even tried using the absolute path to a folder on my desktop and once again they load in fine in my project but they do not get loaded when opening the jar. please help ive tried everything. thanks
(btw) if i open the jar file in intellij the images load but if i open the jar file in finder or from my desktop they do not. i want to be able to send the finished project to someone
The toolkit.createImage(String) method takes in a string, and interprets it as a path, looking for a file at that path. An entry in a jar file is not itself a file. It is therefore impossible to use this method to read image files that are in a jar.
However, that's not the only createImage method. There's also toolkit.createImage(URL) and that is the one you want.
SomeClass.class.getResource("something.png")
This expression works on any class (Foo.class gets you the class instance for Foo, and all class instances have the getResource method), and will look for the named entry in the exact same place SomeClass.class (the file) lives. If SomeClass.class currently lives in a jar file, then that's where it'll look.
Thus, ensure that img.png is in the same place your class file is (ensure it is jarred along with the rest), and that will work. You can also ask for e.g. SomeClass.class.getResource("/foo/bar/img.png"); this will then look from the 'root' of where SomeClass is. So if you have a jar such that if you run jar tvf thatjar.jar and you get:
....
/com/foo/yourpackage/SomeClass.class
....
/foo/bar/img.png
then /foo/bar/img.png works.
thus: Once you know where you put that stuff in your jar file:
toolkit.createImage(YourClass.class.getResource("image1.png"))
is what you're looking for.
I am gettting a nullpointer excetion when i run the applet in browser, while its working fine through appletviewer.
titleicon=new ImageIcon(this.getClass().getClassLoader().getResource("image.jpg"));
img=new JLabel(titleicon)
File locations: Both .class file and image file are in same folder.
C:\project\game.class
C:\project\image.jpg
I tried differennt variations below, none is working in browser but all are fine with appletviewer:
titleicon=new ImageIcon(this.getClass().getResource("image.jpg"));
titleicon=new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));
With a file location of
C:\project\game.class
C:\project\image.jpg
Class.getResource will never work, as this searches the classpath for the required resources.
The use of getResource assumes that the resources are embedded and are relative to the class path.
Without more details, it's difficult to make an accurate suggestion, but I would suggest that you get the images jared without your application code, this way, it will be easier to find.
If you're not using an IDE capable of Jaring your classes and resources, then you will need to have a look at Packaging Programs in JAR Files
Remember, Class#getResource is capable of doing a relative lookup. If the images are not stored in the same package as the class which is loading them, then you need to use an absolute path instead...
I have a relatively basic java program which uses a system tray icon. The path I was using while writing the code is as follows "../images/logo.png". However, when I compile it into a jar file, the image does not show up in the system tray. Instead, if I change the path to "./images/logo.png", then the image shows up in the system tray when it's in the jar file form, but not while I'm testing.
It's not a major issue. However, I am curious to know why this inconsistency occurs.
When you package your program into a .jar file, your build is most likely copying the image into the same directory as the .jar file. However, when debugging in your ide, your image file lies one directory below.
Another possibility is that you are simply setting your Working Directly differently in the two scenarios.
Incidentally, you might be interested in embedding the image in your jar file, see:
https://stackoverflow.com/a/1096491/24954
This answer depends on two things. If the image file is embedded or not.
Embedded Resource
Once you have Jar'ed your application and the images are emebbed inside the application, normal file access methods will no longer work.
Trying to do something like...
new ImageIcon("../images/logo.png");
or
new File("../images/logo.png");
Won't work. This is because the resource is no longer a file within the context of the file system (it's actually a Zip entry in the Jar).
Instead, you need to use Class#getResource which will return a URL to the embedded resource.
new ImageIcon(getClass().getResource("../images/logo.png"));
Will work better. In general though, it is recommended to use an absolute path to the resources new ImageIcon(getClass().getResource("/images/logo.png")); as it's generally more difficult to break (IMHO)
External Resource
The path to the image is relative to the execution point of the application.
In development, you may have had to move up a directory (out of the src folder presumably) to find the image resource. This will mean that you will need to store you Jar file in a folder that would require it step up one level before it could find the image resource.
If you can, it's generally better to embedded the resource within the Jar where possible. It makes it easier to deploy as you reduce the number of files you need to package and makes it (a little) harder for the user to mess with it ;)