Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help loading images in an Executable JAR.  (Read 4287 times)
0 Members and 1 Guest are viewing this topic.
Offline uraknai

Senior Newbie

« Posted 2006-08-18 10:35:11 »


I've developed an application using netbeans that needs to load and display images.

I use netbeans Clean and Build function to create an executable Jar file. When I run the executable Jar file it runs fine but when I come to a section of the application that needs images it freezes and can't load the images.

When I then return to netbeans the part of the program that did successfully run before Clean and Build doesn't work anymore and I get an error message saying Uncaught error fetching image:

I use,

URL url = getClass().getResource("images/image1.png");
Image image1 = Toolkit.getDefaultToolkit().getImage(url);

to load an image.

I've opened the JAR using winzip and, for some reason, the JAR hasn't preserved my file structure. So, when I try to look for an image as follows:

URL url = getClass().getResource("images/file1.png");
Image img= Toolkit.getDefaultToolkit().getImage(url);

The folder doesn't exist and so it can't find the image.

Can someone tell me how to keep my file structure when I create the JAR or an alternative way to find the images within the JAR file.

Thanks Wink

Offline Kova

Senior Devvie

« Reply #1 - Posted 2006-08-18 11:41:24 »

obviusly you don't know how to build a jar with netbeans... make a jar by yourself first (command line), that surely works.
Offline uraknai

Senior Newbie

« Reply #2 - Posted 2006-08-18 13:02:08 »

You're right, I don't really know how to make a JAR using netbeans. Could you possibly tell me why the method I used to create a JAR doesn't load the images whereas it runs fine within netbeans.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Devvie

« Reply #3 - Posted 2006-08-18 15:15:57 »

sorry no, I use eclipse.... but I make jars with command prompt anway, I'm sure someone here knows about it and will help you

so not to waste posts, here's how you do it in command prompt
go to your project workspace dir, where you have dirs of packeges you work with
create a file called "" and put into it your main class, like "Main-Class: kpii.KPII" where kpii is a package and KPII is a class containing main method of kpii package. Think you need to start new line also after that parametar or it wont be recognized.

From command prompt run something like this:
"jar -cfmv kpii.jar kpii info"

for -cfmv as parametars you can see explanaitons with running only "jar"
kpii.jar is name of your JAR file is reference to your manifest file you created which points to main class that will be running when you execute .jar file
kpii and info, 2 last parametars are directories of packeges you include to be added to .jar file

that is it, not complicated at all
Offline uraknai

Senior Newbie

« Reply #4 - Posted 2006-08-19 10:34:38 »

Thanks for your help. I just have a few questions.

My project folder is called Maths.
Within this folder are the folders build, dist and src, with src containing my package folders as well as image folder.

I created a manifest using notepad as follows:

Manifest-Version: 1.0
Main-Class: Menu.Login
Created-By: 1.5 (Sun Microsystems Inc.)

Where Menu is the package name (located in the src folder) and Login is the name of the class with the main method to be run.

I save the manifest in the Maths folder (I'm not sure whether it should be saved here or in one of the other folders)

From the DOS prompt I change the directory to the Maths folder (where th manifest is located) and type:

jar cvfm myapp.jar manifest.MF *

It then compresses all the files and creates a JAR.

The problem is that  when I click on the JAR it says "Could not find the main class. Program will exit".

Could you tell me why this happens. Is there a problem with my manifest or how I create the JAR or something else?


Offline Kova

Senior Devvie

« Reply #5 - Posted 2006-08-19 11:37:14 »

you have to have manifest file in the same folder you execute jar command, so that's in src folder ... or when you enter manifest file parametar you can enter full path to it.
btw. package names should be lowercase by standard

other thing, if you're using windows make sure you entered path with "/" between dirs and not "\" when accessing images (or any other files)... second will work when compiling on windows but wont work in jars.
Pages: [1]
  ignore  |  Print  

EgonOlsen (79 views)
2018-06-10 19:43:48

EgonOlsen (59 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (261 views)
2018-05-13 18:23:11

nelsongames (159 views)
2018-04-24 18:15:36

nelsongames (158 views)
2018-04-24 18:14:32

ivj94 (901 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (813 views)
2018-03-24 14:43:53

Solater (177 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!