So I have an application I have gotten to the prototype stage. It works pretty good too. I got the multithreading to work, the network I/O, the user interface and all that working great. Now my problem is deployment.
I am trying to create a JAR file that is executable. I got this to work, but no entirely. I have some images that I would like to include in the distribution, but I cannot figure out how to reference them in the jar file.
Within the JAR I put them in a folder called img. So I tried using this to reference them with no luck.
To be honest I am pretty rusty with paths in Java. What I need to do is reference the images using a relative path to the jar file. Could anyone show me how to do so?
Also, while I am on the topic do I need to do antyhgin special to make the paths cross platform. For example do I need to consider the windows \ vs the everyone else /?
Finally, I was wondering how I would go about bundling a JRE with my program. I can't know for sure that the user will have Java installed, so I would like to bundle it just in case. I am specifically targeting the latest JRE.
Originally posted by Daniel Chemko: new File(abc);
That won't work because there is no File constructor that takes a URL - only URI. And URL is not a subclass of URI.
However, you probably don't need File objects to show the image. ImageIcon has a constructor that takes a URL, and works like a charm with URLs from resources.
As for platform independance, File can handle both / and \. I use / mostly, even on Windows, and there are no problems with that. However, you might want to use the following: System.getProperty("line.separator") for the newline character (\n, \r, \r\n) File.pathSeparator(Char) for the string (character) between files (;, File.separator(Char) for the string (character) inside paths (/, \) [ March 04, 2008: Message edited by: Rob Prime ]
A slight modification of Daniel Chemko's example:You can't get a File object because entries in jar archives aren't files in the operating system. But you should be able to use this input stream in any code that handles images. (Don't forget to close it after you use it.)
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, the permaculture playing cards