File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Manifest/.jar Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Manifest/.jar Trouble" Watch "Manifest/.jar Trouble" New topic

Manifest/.jar Trouble

Thomas Kiersted

Joined: Feb 24, 2007
Posts: 22

Hello everyone. I'm trying to make an executable .jar file, but cannot seem to figure out the manifest. I've Googled, checked, and checked recent topics here, but haven't figured it out yet. I'll start with all the information about the files:
*I have HelloWorld.class,, and HelloWorld.ctxt all in A:\Hello . The class runs fine in the editor I'm using, BlueJ. HelloWorld.class just prints "Hello World" within the public static void main(String[]args).

I'm trying to create the .jar file using MS-DOS and would like to succeed at that before trying Eclipse or another program. I have added the necessary files to the System Path, and overcome that obstacle. Here is what I am trying in MS-DOS:
1. Change directory to A:\Hello
2. Create manifest file by typing 'echo Main-Class: HelloWorld >manifest.txt'
3. Create .jar file by typing 'jar cvmf manifest.txt HelloWorld.jar HelloWorld.class'

I then try to run the .jar. When I type HelloWorld.jar in the command prompt or double-click the .jar from Windows Explorer, I get the error 'Could not find the main class. Program will exit.' When I type java -jar HelloWorld.jar I get the error 'Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld'. I'm lost as to what to do, but I think it's with the manifest file. I've tried the following modification:

1. Adding 'ClassPath: .\Hello.jar' at the beginning of the manifest file using notepad. I don't think this is necessary, as the manifest file is in the same directory as the class file, but I'm not sure. However, when I create the Manifest file using notepad, I get the error ' invalid header name: (unusual characters)Main-Class at...'.

To summarize, the main problem is that I cannot figure out the error 'Could not find the main class. Program will exit.' I think this is being caused by an error I am making in the manifest file; my second guess is it has something to do with how I have the files set up in the directories.

Thanks alot. This has me bamboozled.

Thomas Kiersted
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Thomas Kiersted

Joined: Feb 24, 2007
Posts: 22

I noticed that FAQ and entered 'jar cvf Hello.jar HelloWorld.class' as it suggested. However, when I then attempted to run the file Hello.jar, I received the error:

'Failed to load Main-Class manifest attribute from A:\HELLO\Hello.jar'
with the header 'Java Virtual Machine Launcher'

I expected that entering 'jar cvf Hello.jar HelloWorld.class' would correctly create the .jar file and only tried to add a manifest afterwards. I still do not understand why this is not working or how to fix it. Is a manifest file required if there is only one class? If not, why isn't the above line working? If so, why did the manifest file I created with the echo line not work?

Thomas Kiersted
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
just manually create a file callled manifest.txt

and inside the file type:

Main-Class: <your class name here(with out .class ext)

and a couple of returns, it will error with out returns.

and put this file in the same directory of the file that you want to

and in command prompt do the following

get in the desired directory, example:

C:\documents and setting\owner> cd desktop\myjavafiles

C:\documents and settings\owner\desktop\myjavafiles> jar cmf manifest.txt myfirstjar.jar one.class two.class three.class

then your jar file is created!

now if you dont have an executable class such as "GUI" the file wont execute when you double click on it. you'll have to open it through cmd

by the way..

when you type cmf

you declaring that you want to

1. c - create a jar file
2. m - include a manifest you created
3. f - followed by the files you want to add

you MUST do it in that order, and dont forget to put the name of the
desired jar file after the manifest.txt, Example:



You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
v - stands for verbose output.

this means that it will list the files that are zipped and the size of them
as it adds them to the .jar file

the reason you're getting the mainclass error is because you dont have a manifest included which tells the jar which class to execute as main.

cmf is what you need

jar cmf manifest.txt helloworld.jar helloworld.class

now if helloworld isn't a gui, it wont run on double click,
you'll have to run it through cmd.

jar -jar helloworld.jar
or it might be:
java -jar helloworld.jar

Thomas Kiersted

Joined: Feb 24, 2007
Posts: 22

Thanks for the reply. Unfortunately it still is not working. I'll probably download Eclipse or another Java editor at some point and try creating a .jar file with that. The create jar command from my current one, BlueJ, didn't create But just in case there is some evident error I'm missing, here are the full contents of the files I'm working with:
1/2. A:\Java\Hi.class - This is the only class in the directory. There also is a file. contains:
public class Hi
public static void main(String[] args)

3. manifest.txt. It contains "Main-Class: Hi" (no quotes) followed by five carriage returns.

So I went into cmd.exe. Here is what I typed/what it replied with

Volume in drive A has no label.
Volume Serial Number is 0000-0000

Directory of A:\Java

02/26/2007 04:27 PM <DIR> .
02/26/2007 04:27 PM <DIR> ..
02/26/2007 04:42 PM 105
02/26/2007 04:42 PM 501 Hi.class
02/26/2007 04:33 PM 24 manifest.txt
3 File<s> 630 bytes
2 Dir<s> 219,648 bytes free

A:\Java>jar cvmf manifest.txt hi.jar hi.class
added manifest
adding: hi.class<in = 501> <out = 317><deflated 36%>


A:\Java> (it is right after this appeared that the Java Virtual Machine error above appeared it a Windows message box)

It occured to me that an unintended character may be in the manifest.txt file. Thus, I checked this:

A:\Java>edlin A:\Java\manifest.txt
End of input file
1:*Main-Class: Hi
//I think this is how it should display, unless the asterisk in line 1 shouldn't be there.
Abort edit?y

Another check on A:\Java>dir resulted only in an entry of 786 hi.jar being added.

I've tried a few variants on this, but if the error isn't in the above I don't think I'll be able to fix it other than in person. If I made an egregious error up there, thanks for the alert. If not, eventually I'll try it in Eclipse or something like that.

Thanks again

Thomas Kiersted
I agree. Here's the link:
subject: Manifest/.jar Trouble
It's not a secret anymore!