I just came up with an error in Java (using Eclipse). I want to load an image from the resource folder into the application. Using the follwoing lines:
URL url = this.getClass().getClassLoader().getResource("/resources/images/icon.png");
BufferedImage i = ImageIO.read(url);
But this results in a java.lang.IllegalArgumentException: input == null! exception.
My folder structure is:
How can I access this image? Thank you a lot!
getResource() returns null if it can't find the resource on the classpath.
In order to use getResource() you need the resources to be on the classpath. The resources directory isn't on the classpath. In Eclipse, you could add the resources folder to the classpath. Or create a new package images under srcServer and move the icon out of resources and into srcServer\images along with your source code.
Another way would be to load the image using a File rather than loading it as a classpath resource.
I believe the reason why it doesn't find the resource is due to your syntax. getClass().getClassLoader().getResource() takes the input without the leading '/' and always starts at the root of the classpath. getClassLoader().getResource() is always an absolute path, whereas getClass().getResource() is a relative path.
Just use:
URL url = this.getClass().getResource("/images/icon.png");
Related
The line
andImg = ImageIO.read(getClass().getResource("gate_and.png"));
fails with
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
I'm using Eclipse and in the navigation view under the bin folder there is the file gate_and.png, suggesting that the file is in the build path.
In the package explorer view I have
project/src/view/class - This is the class that has the code above.
and
project/images/gate_and.png
I right clicked the project folder > build path > link source to add the images folder as a source, doing this again provides a confirmation msg that says images is already in the source.
I have also tried changing gate_and.png to images/gate_and.png and /images/gate_and.png, but since the image gate_and.png is in the bin folder, I think the original is correct.
Assuming your class is in package view.random.name, then
getClass().getResource("gate_and.png")
will look for the resource in
/view/random/name/gate_and.png
relative to the root of the classpath. You apparently don't have a resource by that name there.
By setting project/images as a build path entry, Eclipse will include everything in it on the classpath. Therefore, your resource will appear at
/gate_and.png
You can access it with
getClass().getResource("/gate_and.png")
Note the leading / that means start looking at the root of the classpath, ie. it's an absolute path.
All these rules are explained in the javadoc.
I'm having a little difficulty determining if the path in the code below is a relative path or an absolute path. Also, in this case, I'm trying to open an image I placed inside a folder called "img" which is inside my java project directory. Please don't mind the double backslash (\), I know these only works on Windows, the only thing I wanted to ask if this path relative or absolute.
ImageView img = new ImageView(new Image("file:img\\square.png"))
As you can find in the documentation here you can pass to the constructor any URL supported by the URL class, which is the case in your example.
If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.
In your case, it is relative.
The file: URL scheme refers to a file on the client machine. There is no hostname in the file: scheme; you just provide the path of the file. So, the file on your local machine would be file:///~User/2ndFile.html.
Please read this answer for further information.
Basically, I want to include my main JFrame's icon in the JAR file, so not to need to load it from an external location.
To achieve this, I searched about Java's resource system.
What I have done with Eclipse:
I have created a new folder named "res":
I have copied the files inside it using Windows' explorer:
I have made that folder a source folder:
I have written this code:
URL url = ClassLoader.getSystemResource("/res/icona20.ico");
But url is null.
What did I do wrong?
As mentioned you seem to have added res as source folder, so it is a root, not to name, like src.
URL url = ClassLoader.getSystemResource("icona20.ico");
Class loaders use an absolute (case-sensitive) path, without explicit leading slash /....
Relative paths with an obligatory leading slash for absolute paths:
URL url = Xyz.class.getResource("/icona20.ico");
And you might prefer .png instead of .ico as the latter format is not standard in Java SE.
(About common practices.) The build tool maven uses as nice standard the following source folders:
/src/main/java/
/src/main/resources/
/src/test/java/
/src/test/resources/
Your usage of res is reminiscent of MS Visual Studio ;).
The classloader will get the resources starting from each source folder you added to the classpath. Therefore, the URL should be the following:
URL url = ClassLoader.getSystemResource("icona20.ico");
In the utility package as shown in the snapshot, there is a class Constants. In the same directory as of utility there is a folder called sounds, also shown in the snapshot. Till now I have been giving the complete path of the .wav files.
Like W:/UnderTest/Blaah/Blaaaaah/Foo/sounds/file.wav
How should I give the path, so that when I make a .jar file of it, the sound still works. I tried this :
../sounds/Player_1.wav
but it doesn't work and I get java.io.FileNotFoundException: ..\sounds\Player_1.wav (The system cannot find the path specified)
try this....
URL url = Constants.class.getResource("/sounds/file.wav");
the url in which now gets the path of your file....
In cases like this it's best to load the resource relative to the root classpath:
URL soundURL = Thread.currentThread().getContextClassLoader()
.getResource("/sounds/file.wav");
You can also get the resource as a stream:
InputStream soundURLStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/sounds/file.wav");
The main difference between this and Class.getResource is that Class.getResource assumes that the supplied path is relative to its location in the class hierarchy. So given the relative path "/sounds/file.wav", calling getResource from a class utility.Constants, would attempt to resolve the file.wav resource from package utility.sounds. Whereas using the class loader and the same relative path, the resource would be resolved from sounds. A subtle but important difference.
I am using the code:
URL c = ClassLoader.getSystemResource(filename);
to get a path to a text file. It works fine when in the directory src/main/resources. However, it returns null if I attempt to put it in a subdirectory. How can I get the class loader to look in subdirectories?
Just for the kicks:
Did you try ClassLoader.getSystemResource(subDir + "/" + filename);
You have to give the relative path name. It won't search the directories recursively for you as this is expensive and error prone. You would not know what you would get if you had two resources with the same name in different directories.