1

I'm currently making an executable JAR file which I want to be able to send to other computers. I have a loading/splash screen at the start at the program which uses a picture from my computer.

I'm currently using gradle to create the JAR file maybe that could be used?

Otherwise, I was thinking that I would send a zip-file containing the jar file and the picture and then in someway write the path in such a way so that it always finds the picture?

This is how I get the picture for my program.

new ImageIcon("/Users/UserA/Desktop/SheetsQuickstart/AD3.gif")
Jordan
  • 657
  • 3
  • 9
  • 16

2 Answers2

3

Put your image file under a package (com.myproject.images) in your project and refer it like below:-

new ImageIcon(this.getClass().getResource("images/AD3.gif"));
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • Thank you but I don't really understand how to do this? Where do I create this package if I'm using Gradle? I'm very new to using Gradle and packaging in general. – Jordan May 10 '18 at 16:31
  • @Jordan at the top of your code above imports: **package com.myPackageName.images** – Sean May 10 '18 at 16:36
0

Put the file containing your icon in your classpath (i.e. with your classes in your jar file) and use getResourceAsStream to access your image as a bytestream when you need it.

Note that this is notoriously hard to get right the first time. You might want to look at getResourceAsStream returns null if you run into trouble.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347