Running Jars

R

rob hadow

I have been experimenting with making my own JAR files.
Some of which work and some don't. After hours of altering the application I
haven't been able work out why they sometimes work and sometimes don't.

When the JAR file won't run I get an Exception in thread "Main"
java.lang.NoClassDefFoundError ,but at other times
I get an Exception in thread "main" java.lang.ClassFormatError: MyClass (Bad
magic number).

I create and run the jar file using the following steps:

1/ I create a manifest file, saved as"info"

Manifest-Version: 1.0
Created-By: 1.4.1_05
Main-Class: MyClass

2/ Create a java file "MyClass.java"
Public MyClass {
public static void main(String args[])
{
System.out.println("MyClass running");
}
}

3/ Create a class file "MyClass.class"
javac Myclass.java

4/ Test the application... java MyClass

5/ Create a Jar file .........jar cmf info MyJar.jar MyClass.class
6/ Run the jar file...........java -jar MyJar.jar

I don't understand why it compiles and runs ok, but i get exceptions thrown
when i attempt to run the jar.
I would be very grateful for any help!
?????????

Rob Hadow
 
A

Andrew Thompson

I have been experimenting with making my own JAR files.

Greate idea. They are wonderful things.
1/ I create a manifest file, saved as"info"

Manifest-Version: 1.0
Created-By: 1.4.1_05
Main-Class: MyClass

Unfortunately, I think you are already going wrong.

It is generally better not to 'save your own'
Manifest.mf, but allow the tools to make it
for you.

I do not find Sun's docs on the matter very clear,
<http://java.sun.com/docs/books/tutorial/jar/basics/manifest.html>
but you might get more from the Java Glossary.
<http://mindprod.com/jgloss/jar.html#MANIFEST>

HTH
 
R

rob hadow

Thanks for your reply, and sending me those sites.
I tried creating a jar without first writing a manifest file.
jar cf MyJar.jar MyClass.class
java -jar MyJar.jar

The response was "Failed to load Main Class manifest attribute from
MyJar.jar"
What tools do I use to get the manifest file made for me?

Rob Hadow
 
C

Chris Uppal

rob said:
I have been experimenting with making my own JAR files.
Some of which work and some don't. After hours of altering the
application I haven't been able work out why they sometimes work and
sometimes don't.

One thing to watch out for is that the jar tool seems to ignore the last "line"
of the manifest input file if it doesn't end with a line terminator (DOS or
UNIX style is OK -- or MAC-style for all I know).

Some editors (PFE for Windows, for example) have a nasty tendency to loose the
terminator of the last line, which will cause the kind of intermittent problems
you are seeing.

The easy way to verify is to extract the META-INF/MANIFEST.MF file from the
jar, and see if the Main-Class: line is present.

(You might want to watch out for using unpackaged classes in Jar files too -- I
don't know of any specific problems it causes, but its not best practise and
may trip you up someday)

-- chris
 
I

Igor N. Kolomiyets

5/ Create a Jar file .........jar cmf info MyJar.jar MyClass.class
6/ Run the jar file...........java -jar MyJar.jar

I don't understand why it compiles and runs ok, but i get exceptions thrown
when i attempt to run the jar.
I would be very grateful for any help!
?????????

Try to copy your MyJar.jar into some other folder and unpack it:

jar xvf MyJar.jar

Then check what is inside of the MANIFEST.MF file. Does it contain the
Main-Class: token. If so, make sure that 'Main-Class: MyClass' line ends up
with the <CR> .

Best regards,
Igor.
 
M

Mark Sandford

rob said:
Thanks for your reply, and sending me those sites.
I tried creating a jar without first writing a manifest file.
jar cf MyJar.jar MyClass.class
java -jar MyJar.jar

The response was "Failed to load Main Class manifest attribute from
MyJar.jar"
What tools do I use to get the manifest file made for me?

Rob Hadow

When you create a jar in this manner a default manifest is created,
which contains no Main-class attribute. Just create a file containing
the line

Main-class: relative.path.to.the.main.class

and update the manifest of the jar with the command

jar umf manifest.file jarfile.jar

Hope this helps, MS.
 
R

rob hadow

Igor, thanks for your reply!
I extracted the contents of the file, and this is what I
got.................
jar xvf MyJar.jar
created: META-INF/
extracted: META-INF/MANIFEST.MF
extracted: MyClass.class
Is this as expected?

Rob
 
R

rob hadow

Mark, thanks for your reply. I did what you said and got the following....

Exception in thread "main" java.lang.NoClassDefFoundError:
relative/path/to/the/main/class
Rob Hadow

I
 
I

Igor N. Kolomiyets

Yes, but can you check what is inside of the MANIFEST.MF?
Does it contain: Main-Class: MyClass?

Best regards,
Igor.
 
E

Enrique

rob hadow said:
Thanks for your reply, and sending me those sites.
I tried creating a jar without first writing a manifest file.
jar cf MyJar.jar MyClass.class
java -jar MyJar.jar

The response was "Failed to load Main Class manifest attribute from
MyJar.jar"
What tools do I use to get the manifest file made for me?

Rob Hadow

There is nothing intrinsically wrong with editing the manifest; you
just need to know what you're doing. The technical details are well
documented, so you really should read up before you attempt to change
anything.

The NoClassDefFoundError means exactly what it sounds like. The JRE
cannot locate the class you've generated. Make sure you have your
classpath set so that it can be found -- even if it's the local
working directory (a common beginner mistake).
 
M

Mark Sandford

rob said:
Mark, thanks for your reply. I did what you said and got the following....

Exception in thread "main" java.lang.NoClassDefFoundError:
relative/path/to/the/main/class
Rob Hadow

I

I didn't mean copy the line exactly, replace
relative.path.to.the.main.class with the relative path to the main class
(relative to the location you are executing from, with a jar this would
be the root of the jar file). In your case this would be:

Main-class: MyClass

then update the manifest as before.

MS.
 
J

Jorch

Hi Rob!!

I have bang my head into wall also because these not working jars and
guess what... I think I found the broblem... Or bug...

My "Main-Class: MyClass" declaration was, like you have, on the last
line of the Manifest.mf file WITHOUT new-line mark after Main-Class
line. When I hit enter by mistake jars started to work!! =)

Hope this helps you!

-jori luoto



I have been experimenting with making my own JAR files.
Some of which work and some don't. After hours of altering the application I
haven't been able work out why they sometimes work and sometimes don't.

When the JAR file won't run I get an Exception in thread "Main"
java.lang.NoClassDefFoundError ,but at other times
I get an Exception in thread "main" java.lang.ClassFormatError: MyClass (Bad
magic number).

I create and run the jar file using the following steps:

1/ I create a manifest file, saved as"info"

Manifest-Version: 1.0\n
Created-By: 1.4.1_05\n
Main-Class: MyClass\n <----- !!!!
 
A

Andrew Thompson

My "Main-Class: MyClass" declaration was, like you have, on the last
line of the Manifest.mf file WITHOUT new-line mark after Main-Class
line.

I keep wondering.. how many hours, day,
*man* years of effort has been spent
tracking down that particular bug.

Would it *really* have been that much more
difficult to program the tool read the last
(friggin') line, and ignore if blank!?
 
J

Jorch

I keep wondering.. how many hours, day,
*man* years of effort has been spent
tracking down that particular bug.

I add counter by 3 hours... ;)
Would it *really* have been that much more
difficult to program the tool read the last
(friggin') line, and ignore if blank!?

I think there has been idea to ignore "last empty line"
without checking it because "it's allways there"...

for(int c=0 ; c < args.length()-1 ; c++) or so...

-jori
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top