#### Archived

This topic is now archived and is closed to further replies.

# [java] Jar Files

This topic is 5208 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My program(Level editor) works fine when I run it like this
java ELE
but when I got to jar it, it goes Failed to load Main-Class attribute from ELE.jar.. Here's what I'm doing.. The jar commands:
   jar cvmf add.txt ELE.jar ELE.class ELE*.class default
   Main-Class: ELE
trying to run it:
   java -jar ELE
Why won't it load? Oh and if this'll help the other files are ELE1.class ELE2.class..ELE10.class sorry for lack of originality of naming my stuff.. And I checked the jar file and all the files are there.. [edited by - eedok on October 7, 2003 5:25:20 PM]

##### Share on other sites
Make sure you have a newline after all the lines in the manifest file you''re giving it... Jar (the program) is a little stupid if you don''t, and will give you exactly the symptoms you''re describing.

##### Share on other sites
thx whoever you are, but now I''m getting null pointer exceptions when I go to load images.. This is starting to get annoying..

##### Share on other sites
Post some code outlining the problem, that way you can really get some help on it. The remainder of this post is a complete shot in the dark...

the classpath and all things associated with it (like manifests) are a ripe pain at first, but once you get them, they're cake.

your null pointers are possibly because the image's path is not in the classpath, that is if you're loading from the classpath (which is the easiest way to do it). Something like...

InputStream is =   getClass().getClassLoader()     .getResourceAsStream("bbb/to/image/in/classpath/image.png");// if (is == null) then classpath doesn't have bbb/.. in itBufferedImage myImage = ImageIO.load(is);

In that case, the folder (or jar, which is a bit more complex) that contains bbb/ needs to be in your classpath. If bbb/ is in the folder myProject/, then your classpath needs to be some equivalent of

someJar.jar:anotherJar.jar:/some/lib/dir:myProject

It all becomes second nature after a while, once you see how the JVM's using and interpreting the classpath.

[edited by - tortoise on October 8, 2003 2:11:25 AM]

##### Share on other sites
The error:
java.lang.NullPointerException        at javax.swing.ImageIcon.<init>(ImageIcon.java:138)

The line of code which the error appears at..
  ImageIcon Tilez=new ImageIcon(getClass().getResource("/default/1.gif"));

##### Share on other sites
That immediate slash is probably causing issues, I doubt that path should be absolute. Always use relative paths within classpath references, and their ''roots'' can be any entry in the classpath.

If that doesn''t help post how you are launching the program, your classpath, where you''re launching it from, etc.

Also, just a little note, you should almost always use getClassLoader(), as in

there are situations when the class loader can get the resource, but the class can not. This primarily is an issue when custom class loaders come into play, but with older JVM''s, or unusual ones (I know the JVM on the Sharp Zaurus is one), the getClass().getResource() may return null but getClass().getClassLoader().getResource() would not. It''s just safer (I''ve been bitten by this at unexpected times).

##### Share on other sites
Umm... maybe this is a noob question, but shouldn''t the backslashes be doubled? like:

ImageIcon Tilez=new ImageIcon(getClass().getResource("//default//1.gif"));

instead of what you have? It may be irrelevent and immaterial... but it still bugs me

##### Share on other sites
quote:
Original post by TreizeSG
Umm... maybe this is a noob question, but shouldn''t the backslashes be doubled? like:

ImageIcon Tilez=new ImageIcon(getClass().getResource("//default//1.gif"));

instead of what you have? It may be irrelevent and immaterial... but it still bugs me

What you are using is a forward slash. Forward slashes do not have to be doubled. If you were using a backslash(\), then yes it would have to be doubled.

You may also want to drop the initial ''/'' from the path, like this:

First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

##### Share on other sites
check this out...
To jar or not to jar

Glass_Knife
I think, therfore I am.
I think?

##### Share on other sites
that article doesn''t help at all..
getClassLoader() and relative paths don''t work either..

##### Share on other sites

First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

##### Share on other sites
my source is over 2000 lines long.. And the only problem I have right now is images having a null pointer exception..

I''ll e-mail you the jar file if you really want me to.

##### Share on other sites
1. capitalization is very important, worth doublechecking

2. for games you generally shouldn''t use imageicon for loading images, i launches a mediatracker and stuff behind the scenes and is therefore uneccesarily slow

3. i think gif support is either non existant or bugged

4. this code works no matter if the stuff is in a jar or not (dunno how it works out across multiple jars):

------------------------------------------------

URL u = getClass().getResource("bla/pic.jpg";
System.out.println(u);

BufferedImage i;
System.out.println(i);

------------------------------------------------

-smoke

##### Share on other sites
1.The capitalization is 100% correct, or else it wouldn''t work out of the jar file.
2.It''s for my level editor, speed isn''t an issue for that.
3.What format should I use then?
4.That changes the null pointer exception to java.lang.IllegalArgumentException.

##### Share on other sites
quote:
Original post by eedok
my source is over 2000 lines long.. And the only problem I have right now is images having a null pointer exception..

I'll e-mail you the jar file if you really want me to.

Sure. Send it to me in an e-mail. Just put into a zip/jar so that it will be compressed. Include the images in the zip as well.

First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

[edited by - CaptainJester on October 16, 2003 7:16:33 AM]

##### Share on other sites
quote:
Original post by Anonymous Poster
2. for games you generally shouldn''t use imageicon for loading images, i launches a mediatracker and stuff behind the scenes and is therefore uneccesarily slow

3. i think gif support is either non existant or bugged

I use ImageIcon with gifs all the time and it works flawlessly. The only ''bug'' is that certain formats of gif don''t support transparency, but thats a limitation of the file format, not ImageIcon.

##### Share on other sites
quote:
Original post by Anonymous Poster
2. for games you generally shouldn''t use imageicon for loading images, i launches a mediatracker and stuff behind the scenes and is therefore uneccesarily slow

3. i think gif support is either non existant or bugged

2. MediaTracker is only used until the image is loaded. It should not affect performance.

3. There is full gif and jpeg support in Java. I have never had a problem with either.

First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

##### Share on other sites
sorry for the gif comment. loading gifs produced some strange errors when i tested above code snippet but jpes worked perfectly; today both work fine... :/ a bit quick to cry bug.

the imageicon class has a static mediatracker which requires a component (static too). so imageicon sure has some startup cost but thats pretty much one time and shouldn''t matter if you use awt/swing anway but if you use other stuff for display i''d avoid it. (not sure but wouldn''t instantiating a compontent start a awt-event-dispatch-thread?)

##### Share on other sites
quote:
Original post by smokew
not sure but wouldn''t instantiating a compontent start a awt-event-dispatch-thread?

I don''t think so. There is only one event dispatching thread that handles all events. But there is always a cost of creating an instance of a class. That''s why you should try to create all instances outside of time sensitive code.

First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte