I am developing JNLP Application and using maven-webstart plugin to create JNLP. When running the application in my local, it works correctly but when I run in Tomcat Server using jnlp, it does not load images and also does not give any exception.
I am loading images as below
new ImageIcon(getClass().getResource("/icons/save.png"))
What is wrong with my code?
Where are your icons located? How do you run a jnlp application inside tomcat? Assuming you don't have a lot of images, you can package them inside one of your packages. Just create a package called com.yourproject.resources and dump some images there. Then the way to access them would be:
//define it in some class
public URL obtainImageResource ( String nameOfResource )
{
return getClass().getResource( "/com/yourproject/resources/" + nameOfResource );
}
Do get them with a URL.
Then to create the stuff you need:
BufferedImage yourImage = ImageIO.read( yourclaass.obtainImageResource( "yourimagepng.png" ).openStream() );
Note some things: In my setup you need to package your images into your jar.
It's hard to say without more information. You might use jar -tf to see if the images are in the JAR file. In the meantime, here's a working JWS application that loads images, for reference.
Related
I'm working on a game, and I've stumbled across a fews ways to load an image into my program. There are two common ways:
The first (No class folder needed):
ImageIcon ii = new ImageIcon("image.png");
Image i = ii.getImage();
When using this method, you simply add a folder to your Project Folder (In Eclipse) and DO NOT have to make it an external class folder.
The second option (Class folder needed):
Image i = ImageIO.read(ImageLoader.class.getResource("image.png"));
You can only load images this way if you made a resource folder (typically called 'res') and added it as an external class folder to your project.
My question is why would you use the second option instead of the first? From my limited experience, it seems like the first option works just the same as the second, just without having to turn the folder you're adding it to into an external class folder.
The first option doesn't work if you package your application as a jar. The second option works in both cases, as an exploded application in an ide or on the hard drive and packaged in a jar file.
new ImageIcon("image.png"); assumes that the image is stored on the file system (in your case, in the current working directory), which raises up some issues - as the working directory isn't always the same location that the jar is stored.
ImageIO.read(ImageLoader.class.getResource("image.png")) embeds the image within the Jar file which resolves the issues associated with the first method as the image is where ever the Jar file is.
It also simplifies installation, as you only need to track the jar files and not all the other "external" files.
ImageIcon doesn't report the reasons it fails. It loads the images in a separate thread, which make diagnosing issues difficult.
ImageIO loads the image in the current thread (and returns a fully realised image) but will also generate a IOException if the image fails to load for some reason, making a much better way to load images in general. It also supports (or has the capacity to support) a wider range of images.
In general, using ImageIO and embedded images is simpler and easier to work with and is generally the recommended approach for fixed resources
I have a problem which appears when I export to runnable jar.
Basically I am creating a JavaFx Game which loads Images by using:
Image image = new Image(BlueCar.class.getResource("../resources/blueCar.png").toExternalForm());
Everything works fine within eclipse. However when I try to export and run it give a error on the line of code above.
http://i.imgur.com/VbmGSO3.jpg
I am using MVC Pattern for the application so I have packages separating the class from the images resources:
http://i.imgur.com/gzm7d0s.jpg
Please if you could help know what I am doing wrong please ?
Jar filesystem does not support relative paths - use absolute ones getClass().getClassloader().getResource("path/to/resource/blueCar.jpg")
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.
I have a web page hosted on jetty server. The page displays some static images, the images directory are kept inside a jar, say application.jar. This application.jar is packaged inside a one-jar. I am facing problem in accessing the images directory as they are inside a jar which is inside another jar.
I have tried almost all the ways to get URL of the images directory: class.getClassLoader().getResource(), Thread.currenctThread.getContextClassLoader().getResource(), ClassLoader.getSystemClassLoader().getResource(), etc. None of them is of any help. I wrote all these statements, i.e. tried to get access to the images directory, from a class that is inside the application.jar. This jar contains the images directory too.
If anybody has ever faced this before, please reply to this thread. I am open to any other ideas also that may help me achieve the objective.
You can use
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
You can find more here:
How to read a file from jar in Java?
How to access resources in JAR file?
I've written a Snake clone in the process of learning how to program in Java. It is an applet that uses images (loaded in the init() method using getImage(getDocumentBase(), "gfx/image.png");
When I run the applet in my IDE (Eclipse) it runs fine and all the images are shown.
My goal however is to create an executable jar file that I can pass around easier than an applet. So I created a new class and used a JPanel to host my applet. Now the problem is that getDocumentBase() always returns null, resulting in the images not being found on the filesystem, resulting in an empty screen.
I know the game runs cause I can navigate all the menus and see all the text that is printed. It's just the images that aren't loaded.
Is there any way around this? Should I load my images another way?
Thanks
You can load resources from within you jar-file by using the getResource() method from Class. On DevX there's a nice tutorial showing you how to do that for applets and applications:
http://www.devx.com/tips/Tip/5697
There is also an article from Oracle describing how to access resources in a location-independent manner:
http://download.oracle.com/javase/1.4.2/docs/guide/resources/resources.html
Basically you're accessing an image like this:
URL myurl = this.getClass().getResource("/myimage.gif");
Toolkit tk = this.getToolkit();
img = tk.getImage(myurl);
Load your image like this instead:
public void init() {
URL url = getClass().getResource("/gfx/image.png");
Image image = getImage(url);
}
Then add your gfx/image.png file to your jar and keep the path. Note that jar files are just zip files.