Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jar File/Deployment Question

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

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.

Thanks,

Jon
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the jar file you're running is already in your classpath, you should be able to type something like URL abc = this.getClass().getResource("/img/abc.jpg");
new File(abc);
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help so far.

Could you provide an example of how to do this?

Keep in mind that the file I want to get a hold of is in the jar file. It's in a folder called img that is within the jar.

How would I go about pulling this image out of the jar file? Perhaps it would be easier not to include it in the jar, just put it in the program folder.

Thoughts welcome.

Thanks,

Jon
 
Marshal
Posts: 28145
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic