0

I have just finished my first proper java program with a GUI, and have set all of the options in netbeans in order for the project to create a .jar file which is currently displaying my Frame and connecting to the database. The issue I am having is that when I run the program (I have to run it from terminal as clicking the .jar might be part of the issue?), none of my images load. I have tried the following;

  1. Going into project Library preferences, and adding the /img folder which resides within my P3Game folder (root dir of the project) with no avail.
  2. Tried directly copying the images to the generated /dist folder but then I cant add the images via the library pref's due to it not being a folder or library.
  3. Manually copying over the img folder into dist (after compiling) and setting the file path to dist/img/imgName.png and it worked, however, any time I build/ compile the project, the img folder within dist gets completely wipes (just the contents)

ANY help would be much appreciated.

EDIT1: this is how im adding the images into the program, and this works from netbeans, but not from running the program elsewhere.

public void setGrassIcon() throws IOException {
        this.grassIcon = new ImageIcon(ImageIO.read(new File("img/Grass.png")));
}
Taylor Styles
  • 139
  • 1
  • 13
  • 1
    What build system are you using for this project? Maven? Gradle? Ant? Something else? – Jeroen Steenbeeke Oct 19 '20 at 08:28
  • I am using an ANT project – Taylor Styles Oct 19 '20 at 08:28
  • 1
    Sounds what you want is a jar including all dependencies. Just google for "fat jar". If I would start a new project anyway I would prefer gradle over ANT. But anyway this is also possible with ANT. E.g. here: https://stackoverflow.com/questions/1821803/creating-a-bundle-jar-with-ant – timguy Oct 19 '20 at 08:33
  • Is it easier to just copy all of my code over to a maven/ Gradle project? I am using derby as an imbedded db too. I guess the question is, would copying the code over to a Gradle project solve both my executing problem as well as the image (and how?) and does it still support DerbyDB? – Taylor Styles Oct 19 '20 at 08:41

1 Answers1

1

If you run from command line, the pictures have to be in your classpath:

java -cp "pictures\*"  

How does the code looks like to load the images? Could look like this:

Example.class.getResource("pictures/mypic.png");
timguy
  • 2,063
  • 2
  • 21
  • 40
  • So to add the images, I have them being loaded in a SavedData class which I have updated (see edit1 on post) However, this works perfectly when running the project from netbeans, ONLY when I run it from terminal I have this issue. Also having an error that says to check console when I double click on the program directly... – Taylor Styles Oct 19 '20 at 08:34
  • what exact error did you got? Did you add the classpath with your picture as described? Whats the output if you try to load the image like 'ImageIO.read(this.getClass().getResource("img/Grass.png"));' – timguy Oct 19 '20 at 09:10