This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problems in executing a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems in executing a jar file" Watch "problems in executing a jar file" New topic
Author

problems in executing a jar file

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
I have some problem in executing the jar file... I have following scenario:
1. In normal scenario (if i don't have a jar file)...say I am creating a directory project..this will be my parent dir and i'll have test (this will have all class files) and images (this will have all images) as subdirectory of project. To run my program i have to go to my project dir at command prompt and can run by
c:\project>java test.app
(where app is my main class). and it runs very well.
2. Now I tried creating a executable jar with following cmmand
c:\project>jar cvf temp.jar test/*.* images/*.* entrypoint.mf
while entrypoint.mf has following line with an extra carriage return at the end:
Main-Class: test/app
when I issue following command at the prompt:
c:\project>java -jar temp.jar
my programs runs prefectly fine.Take care it also has test and images folder also with all relative files extracted as this was original folder from where I have created the jar file. But if i copy this jar file into another folder say newProject and try to run program like this:
c:\newProgram>java -jar temp.jar
it runs my program but without the images...why?

1. Is jar file itself not contains all information for every files it has included?
2. Do I have to include images directory and others (except class files) separately?
3. What is a solutions for it? where am I wrong?

Please help me sort the issue..
regards,
Arun
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You need to use ClassLoader.getSystemResource/getSystemResourceAsStream to load your resource files.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Ilja for your reply...but I failed to use it..I tried using the ClassLoader.getSystemResource method but it is giving me error..perhaps I am doing something weird...can you help me in modifying the following code for me..this lies in my class called myapp.java which is under package test.
I am using images in following three fashion..

1. Image img = Toolkit.getDefaultToolkit().getImage("images/picon.gif");
2. JLabel background = new JLabel(new ImageIcon("images/mbg.jpg"));
3.ImageIcon i1=new ImageIcon("images/buttons/go/go.gif");
regards,
Arun :roll:
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
arun,
What do your attempts at using the suggested method(s) look like?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks Dirk for your intrest in this post..I earlier thought of giving code also..but was a bit reluctant as perhaps I was not able to understand the method very well and thought that perhaps asking is better answer(as a matter of fact i was afraid if some one will crib or laugh at me..anyway let me find some courage)..but you're right I *must* put whatever I tried.. here is what I tried...

1. URL url = abc.class.getResource("images/picon.gif");
Image img=Toolkit.getDefaultToolkit().getImage(url);

abc is the class where I am tying to load the image..
It failed to find the image..
2. I than tried with

URL url = new LoadClass().getResource("images/picon.gif");
Image img=Toolkit.getDefaultToolkit().getImage(url);

The Loadclass is like this:

package test;
public class LoadClass extends ClassLoader
{
public LoadClass()
{
super();
}
public LoadClass(ClassLoader c)
{
super(c);
}
}

It worked but failed in second case:

JLabel background = new JLabel(new ImageIcon("images/mbg.jpg"));

3. Tha lastly I tried with this:

Enumeration en=(Enumeration)ClassLoader.getSystemResource("images/picon.gif");
Image img = null;
while(en.hasMoreElements())
{
img=(Image)en.nextElement();
}

but it never compiled..
Can you help me...
regards,
Arun
[ June 10, 2002: Message edited by: arun mahajan ]
[ June 10, 2002: Message edited by: arun mahajan ]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
can somebody pl help me in this.

thanks
Arun
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
I hope someone will find time from this promotion craze and help this poor guy for this post
Arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This conversation is being continued here...
 
 
subject: problems in executing a jar file