I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass().
getClassLoader().getResource("unibo.lsb.res/dice.jpg");
The directory structure is the following:
unibo/
lsb/
res/
dice.jpg
test/
..../ /* other packages */
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue.
Any hint?
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
leading slash to denote the root of the classpath
slashes instead of dots in the path
you can call getResource() directly on the class.
Instead of explicitly writing the class name you could use
this.getClass().getResource("/unibo/lsb/res/dice.jpg");
if you are calling from static method, use :
TestGameTable.class.getClassLoader().getResource("dice.jpg");
One thing to keep in mind is that the relevant path here is the path relative to the file system location of your class... in your case TestGameTable.class. It is not related to the location of the TestGameTable.java file.
I left a more detailed answer here... where is resource actually located
Related
This feels like it should be something straight forward, but I can seem to find an elegant solution to it without converting to File.
Given a Path
Path path = Paths.get("/a/b/foo")
How to do get the path /a/b/foo.bar? subpath will return a relative path regardless of whether the original path is relative or absolute.
I would prefer not to have to use additional libraries. But, maybe that is the only way?
To change the file name of a Path, use one of the resolveSibling() methods:
This is useful where a file name needs to be replaced with another file name.
Using this method ensures that the result Path object is for the same FileSystem as the source Path object.
So, to add extension ".bar" to a Path:
path = path.resolveSibling(path.getFileName() + ".bar");
I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass().
getClassLoader().getResource("unibo.lsb.res/dice.jpg");
The directory structure is the following:
unibo/
lsb/
res/
dice.jpg
test/
..../ /* other packages */
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue.
Any hint?
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
leading slash to denote the root of the classpath
slashes instead of dots in the path
you can call getResource() directly on the class.
Instead of explicitly writing the class name you could use
this.getClass().getResource("/unibo/lsb/res/dice.jpg");
if you are calling from static method, use :
TestGameTable.class.getClassLoader().getResource("dice.jpg");
One thing to keep in mind is that the relevant path here is the path relative to the file system location of your class... in your case TestGameTable.class. It is not related to the location of the TestGameTable.java file.
I left a more detailed answer here... where is resource actually located
I have current working file's directory path I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\WEB-INF\classes\PackageName\
I want path like I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\. How can I do this in java code?
You better get your App-Path from the ServletContext in Servlet-Containern like Tomcat.
Use
servletContext.getRealPath(".");
// will return 'I:\apache-tomcat-7.0.40\webapps\ExecutableFileProcess\' on win.
// will return '/usr/local/tomcat/webapps/ExecutableFileProcess' on unix.
Have a look at
http://developer.android.com/reference/java/io/File.html#getParentFile()
Applying this twice will probably get you the right directory.
But note that you program might or might not be allowed to go there, based on the settings in the file system and the context your code runs in.
Try this:
Path aPath = Paths.get("I:\\apache-tomcat-7.0.40\\webapps\\ExecutableFileProcess\\WEB-INF\\classes\PackageName\\");
Path parentsParentpath = aPath.getParent().getParent();
When I create ImageIcon class objects I use the following code:
iconX = new ImageIcon (getClass().getResource("imageX.png"))
The above code works correctly either in an applet or a desktop app when the .png is in the same folder of the class.
The question is: how to avoid a NullPointerException when the .Png is in another folder? Or how load the image in the object ImageIcon when it is in a different location to the class?
I don't understand how this method works, if anyone can help me I appreciate it. Thanks!!
Take a look at this - Class#getResource(java.lang.String)
Please click the link above and read the docs and follow to understand what's going on.
It says -
If the name begins with a '/', then the absolute name of the resource is the portion of the name following the '/'.
and
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.'.
So, if this object (where you call getResource) is in package /pkg1 (/ meaning pkg1 is right under the root of the classpath) and you used "imageX.png" then the result would be pkg1/imageX.png which is correct because that's where the image is located at.
But, if we moved the resource (imageX.png) to some other package /pkg2 and you called the method same way then the result would still be pkg1/imageX.png but this time it would be incorrect because the resource is actually located in /pkg2. That's when you end up with NPE.
It's good to explicitly specify the full path of the resource starting from the root of the classpath. (e.g. "/pkg/imageX.png").
Hope this helps.
Simply supply the path to the resource.
So, if you put the image in "/resources/images" within your Jar, you would simply use
iconX = new ImageIcon(getClass().getResource("/resources/images/imageX.png"))
Essentially what you're saying is, class loader, please search your class path for the following resource.
If the image is internal (you want a location relative to your project, or perhaps packaged into your jar), do what mad programmer said:
iconX = new ImageIcon(getClass().getResource("/path/imageX.png"))
The path is relative, so path/ will be a folder in the same folder as your project (or packaged into your jar).
If you want an external image, simply hand ImageIcon constructor the path (ex. "C:/.../file.png"). This isn't recommended though, as it's better to use it as a resource.
For more info on the ImageIcon constructor, see here. for more info on loading class resources, see here (Javadoc links)
I want to place my properties files in some folder, but I am not able to read them because we can specify only the bundle name in static getBundle() method on ResourceBundle object.
Suppose bundle is: myFile.properties
Current path is: src
I want to keep my properties file in: src/temp
So when I am using:
ResourceBundle.getBundle("temp/myfile", currentLocale);
it is throwing an exception "can't find bundle". I want some way to specify the path. Please suggest me some way to do this.
Thank you
Use this:
ResourceBundle.getBundle("temp.myfile", currentLocale);
The baseName supplied in the ResourceBundle.getBundle call is supposed to be a fully qualified class name. So it has to be written separated with dots. Also note that this makes temp a package in your java code (which I don't think is a good idea). It is better to put the properties file in a proper package like com.xyz.abc. Then you can access it using
ResourceBundle.getBundle("com.xyz.abc.myfile", currentLocale);
It is possible that you are using the wrong filename (myfile != myFile).
For a file myFile.properties in a directory temp on the classpath, this code should work:
ResourceBundle.getBundle("temp.myFile");