-1

I'm trying to compile everything in a single JAR file. It includes a manifest file, classes I needed for the program, and also the folder where the pictures and icons are located.

Main.class
SubExt.class
Frame1.class
Data.class
Graphics (folder)

Inside the graphics folder, I have 3 sub folders:

Icon
Text
Bg

Icon folder has all the icon files. Text folder has all the fonts used. Bg folder has all the backgrounds used in the program.

I tried compiling it in one single jar file:

jar cvfm PackagingJava.jar Manifest.mf *.class Graphics

If I would try running it on the same directory (where the graphics folder is also located), it would run properly. But when I try running it on different directory (e.g. Desktop), it would not display fonts, backgrounds, and icons correctly. It will just display a blank white background.

I didn't use an absolute path in my program. When I added Icons on my program, it looked like this:

ImageIcon icoSub = new ImageIcon ("Graphics/Icon/a.png");

Also, my font codes looked like this:

Font baseFont;
Font awesomeFont;
baseFont = Font.createFont (Font.TRUETYPE_FONT, getClass().getResourceAsStream("Graphics/Text/awseomeFont.ttf"));
awesomeFont = baseFont.deriveFont (Font.PLAIN, 16);

It doesn't work. What should I do?

  • 2
    `ImageIcon(String)` is looking for the specified resource on the disk, but it no longer exists, as it's been bundled into the Jar file, this means you can no longer reference your resources as "files", but instead need to use `Class#getResource`. Something like `new ImageIcon(ImageIO.read(getClass().getResource("/Graphics/Icon/a.png")));` would be more appropriate. Should also unzip the jar file and make it contains the resources ;) – MadProgrammer Aug 20 '15 at 02:08

1 Answers1

0

you should access the resources the flowing way

ImageIcon iconSub = new ImageIcon(ImageIO.read(getClass().getResource("/Graphics/Icon/a.png")));

for the fonts you can use the following way.

InputStream is = this.getClass().getResourceAsStream("yourFont.TTF");
uniFont = Font.createFont(Font.TRUETYPE_FONT,is);
Font f = uniFont.deriveFont(24f);
Muhammad
  • 5,693
  • 4
  • 39
  • 54
  • I tried this one. But it doesn't applies to fonts. I tried packaging it together with the fonts. I used baseFonts = Font.createFont (Font.TRUETYPE_FONT, getClass().getResourceAsStream("Graphics/FONT/baseFont.ttf")); It doesn't work. – Neo Anderson Aug 20 '15 at 13:47