Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems in executing a jar file

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use ClassLoader.getSystemResource/getSystemResourceAsStream to load your resource files.
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arun,
What do your attempts at using the suggested method(s) look like?
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody pl help me in this.

thanks
Arun
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope someone will find time from this promotion craze and help this poor guy for this post
Arun
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This conversation is being continued here...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic