Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 1977 times)
0 Members and 1 Guest are viewing this topic.
Offline uraknai

Senior Newbie





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

Hi,

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,


1  
2  
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:

1  
2  
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 Duke





« 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.

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

Senior Duke





« 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

EDIT:
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 "manifest.mf" 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 manifest.mf kpii info"

for -cfmv as parametars you can see explanaitons with running only "jar"
kpii.jar is name of your JAR file
manifest.mf 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?

Cheers



Offline Kova

Senior Duke





« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!