I think I\'m suffering from some classpath blues here. I\'ve been following the examples. I read the tutorials. But nothing seems to be working for me.
Let\'s say I
String someIconUri = "C:\Users\MyUser\someIcon.jpeg";
No it is not 'some icon URI', it is not a valid URI at all. Come to think of it, it is not even a compilable statement, since the String
contains illegal escape chars!
It might be something along the lines of..
String someIconUri = "file:///C:/Users/MyUser/someIcon.jpeg";
But then getResource()
is neither needed or useful, since it will only locate resources on the run-time class-path. Just construct an URL
directly from the String
.
But that is really only 'part of an answer'. Here's why.
In this context, there are basically two types of resources. Application resources and (for want of a better word) User resources.
These resources might consist of things like frame icons, button and menu icons (Action
icons), icons for tabs. Help files (and associated images), splash images..
They should be added to a separate Jar (most often) and added to the run-time class-path of the application (either using the manifest or other means like applet element or JNLP file).
Application resources should be accessed by URL, which can be obtained using getResource()
:
URL iconUrl = this.getClass().getResource("/icons/copy.jpg");
The user wants to open (edit/print) an existing text document, make an animated GIF from image frames, edit an image on their file system..
For these types of resources (and presuming the app. is trusted or has no security manager), offer the user a JFileChooser. It will return a specific, existing File
or more (depending on how configured and used).
In that case, never convert the File
to anything else, just use the instance(s) directly.
Most methods that take input from resources (worth mentioning), will accept a File
URL
or InputStream
.
The last is useful for something generated in memory, or obtained from sources such as sockets or a JNLP API FileContents object. The latter is of special interest to sand-boxed apps. launched or embedded using Java Web Start.