[java] Jar Files

Started by
17 comments, last by eedok 20 years, 6 months ago
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  
The add.txt file:
   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]
Advertisement
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.
thx whoever you are, but now I''m getting null pointer exceptions when I go to load images.. This is starting to get annoying..
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]
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"));


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

ImageIcon(getClass().getClassLoader().getResource("/default/1.gif"));

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


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
Project ARPEG: Product, Darkness SeigeThe seige begins...
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:

ImageIcon(getClass().getClassLoader().getResource("default/1.gif"));


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
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
check this out...
To jar or not to jar


Glass_Knife
I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

that article doesn''t help at all..
getClassLoader() and relative paths don''t work either..

This topic is closed to new replies.

Advertisement