Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Executable JAR File?

Executable JAR File?
Thread Tools
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status: Offline
Reply With Quote
Feb 25, 2003, 05:56 PM
 
Hi,

I am developing a Java application, which I am able to pack into a JAR file by using the "jar -cvf target.jar source" command. However, I am unable to make this JAR executable.

I have followed the standard procedure of using the command "jar -cvmf myManifest.mf...", where myManifest.mf is a text file containing the line "Main-Class: ClassName". However, even when I use this custom manifest file, when I try to execute the JAR with the java -jar command, I get an error saying that the Main-Class attribute couldn't be read from the manifest. I also tried expanding the JAR and looking at the manifest, and the Main-Class information was indeed missing from it. Any suggestions? Thanks.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Feb 26, 2003, 01:58 PM
 
Your manifest should go in META-INF/MANIFEST.MF and should look something like this:

Code:
Manifest-Version: 1.0 Main-Class: com.mydomain.myClass Created-By: Anomalous
Then when you create the jar, do the following:

Code:
jar -cmf mymanifestfile myjarfile com/mydomain/myClass
The "m" flag tells it you want to use your own manifest file rather than theirs.
(Last edited by Arkham_c; Feb 26, 2003 at 02:08 PM. )
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status: Offline
Reply With Quote
Feb 27, 2003, 02:31 PM
 
Thanks for your suggestions.

When I tried this, I think I have successfully used a custom manifest file now, but when I try to execute the JAR, I get a "class not found" error.

I'm not exactly sure what you mean by com and mydomain.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Feb 28, 2003, 01:49 PM
 
Originally posted by Anomalous:
Thanks for your suggestions.

When I tried this, I think I have successfully used a custom manifest file now, but when I try to execute the JAR, I get a "class not found" error.

I'm not exactly sure what you mean by com and mydomain.
Most java files are part of a package. At the top of the source there is a line that describes what package it is in, that looks like this:

Code:
package mypackage;
Typically, in order to ensure that your package name is unique across all java classes, the package is prefixed with your domain name in reverse order. For example, my domain is theresistance.net, and I have an Email package with SMTP and POP classes. At the top of all these source files is:

Code:
package net.theresistance.Email;
The code is then stored on the file system in net/theresistance/Email/somefile.java. If I wanted my default class in the manifest to be POPClient.class, then the way it is specified in the maifest is:

Code:
Main-Class: net.theresistance.Email.POPClient
In a simple example, you might not have a package. In this case, just remove all the domain stuff.

I tarred/gzipped up my sample code that I used to write the original reply. It's on my web site here for you to look at:

http://david.theresistance.net/files/jar_classtest.tgz

It's very simple and should remove any ambiguity caused by my poor explanation. Hope this helps.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status: Offline
Reply With Quote
Feb 28, 2003, 02:29 PM
 
Thanks for the sample code. Your explanation is not poor - I am just a little confused. I will keep looking to try to figure out what I am doing differently from your example.
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Feb 28, 2003, 10:25 PM
 
Originally posted by Anomalous:

However, even when I use this custom manifest file, when I try to execute the JAR with the java -jar command, I get an error saying that the Main-Class attribute couldn't be read from the manifest.
I didn't test the OS X jdk, but the Blackdown linux jar command is pretty picky about the last line of your manifest containing a newline. If you don't have one it won't add that attribute. I must have said WTF 10 times in the last five minutes.

Glad to see you got your problem solved.
     
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status: Offline
Reply With Quote
Mar 5, 2003, 02:24 PM
 
Okay, by moving the files around I was able to get an executable JAR file working. However, I tried to embed my program's image and sound files into the JAR, and the program couldn't locate them at runtime. How can I refer to files within the JAR?
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Mar 5, 2003, 02:54 PM
 
Originally posted by Anomalous:
Okay, by moving the files around I was able to get an executable JAR file working. However, I tried to embed my program's image and sound files into the JAR, and the program couldn't locate them at runtime. How can I refer to files within the JAR?
I suspect your answer is here:

http://developer.java.sun.com/developer/qow/archive/76/
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status: Offline
Reply With Quote
Mar 7, 2003, 02:24 PM
 
Originally posted by Arkham_c:
I suspect your answer is here:

http://developer.java.sun.com/developer/qow/archive/76/
Thanks for the link.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Mar 8, 2003, 09:26 PM
 
Originally posted by Anomalous:
Thanks for the link.
Google is great.

Just prepend "site:java.sun.com" as an argument to java-related queries and you can find anything about java in a flash.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:49 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2