By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

Problems creatng executible jar files

P: n/a
This is quite frustrating! In the documents, and in the newsgroups, I see
simple instructions like the following:

===============================================
==========Beginning of Ray's example=================
===============================================
Suppose you want to run class com.xyz.Main. Then create a manifest file
like this:

======== main.mf starts on the next line ====
Main-Class: com.xyz.Main

======== end of main.mf =====================

(Where the lines with === above are not actually in the file.)

Then create your jar file:

$ jar cvfm xyz.jar main.mf com/xyz/*.class

===============================================
=========end of Ray's example=======================
===============================================

With such simple instructions, it is quite frustrating to see the
recommended procedure failing without any information as to what caused the
problem.

I have appended what I see in the command console after following Ray's
instructions, changing only file and class names to match the files I'm
using in my test. No error ever appears in the comsole or when compiling
the code. However, when I try to run it, I get an error message dialog box
saying that the main class could not be found.

I do not know if it makes a difference, but HotSpotImageTest is derived from
JApplet and has function main. It runs fine within JBuilder, and I have
compiled it without problems from the command line using only Sun's SDK.
And it runs fine as an applet, but this jar file business is giving me
grief.

If it makes a difference, this application/applet makes considerable use of
Swing.

Once this problem is resolved, I need to be able to make the jar file self
contained, including in it everything that the application needs to run so
that I do not have to worry about the version of the java runtime on the
client machine or whether or not there is support for Swing on it. IS this
possible? If so, how? I do not really want to be installing a new JVM on a
client machine unless I absolutely have no choice in the matter.

Thanks,

Ted

=======contents of manifest.mf==(including a couple empty lines at the end
of the file)====
Main-Class: HotSpotImageTest

===========Partial session record==================
H:\JavaProjects\HotImage\HotImage\classes\hotimage >dir
Volume in drive H is Local Disk
Volume Serial Number is 686B-F4CF

Directory of H:\JavaProjects\HotImage\HotImage\classes\hotimage

06/04/2004 01:06p <DIR> .
06/04/2004 01:06p <DIR> ..
28/03/2002 11:29p 78,727 daynite.gif
28/03/2002 11:18p 7,667 deerani.gif
31/03/2004 04:07p 4,757 HotSpotImageTest.class
06/04/2004 01:06p 103,281 HotSpotImageTest.jar
31/03/2004 04:07p 683
HotSpotImageTest_jLabel1_mouseAdapter.class
31/03/2004 04:07p 935
HotSpotImageTest_jLabel2_mouseAdapter.class
06/04/2004 01:04p 30 manifest.mf
21/06/2003 04:42a 100,096 sunset.jpg
8 File(s) 296,176 bytes
2 Dir(s) 17,338,773,504 bytes free

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest
'HotSpotImageTest' is not recognized as an internal or external command,
operable program or batch file.

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest.jar

H:\JavaProjects\HotImage\HotImage\classes\hotimage >jar cvfm
HotSpotImageTest.jar manifest.
mf *.class *.jpg
added manifest
adding: HotSpotImageTest.class(in = 4757) (out= 2304)(deflated 51%)
adding: HotSpotImageTest_jLabel1_mouseAdapter.class(in = 683) (out=
358)(deflated 47%)
adding: HotSpotImageTest_jLabel2_mouseAdapter.class(in = 935) (out=
413)(deflated 55%)
adding: sunset.jpg(in = 100096) (out= 99228)(deflated 0%)

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest.jar

H:\JavaProjects\HotImage\HotImage\classes\hotimage >
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ted Byers wrote:

=======contents of manifest.mf==(including a couple empty lines at the end
of the file)====
Main-Class: HotSpotImageTest

===========Partial session record==================
H:\JavaProjects\HotImage\HotImage\classes\hotimage >dir
Volume in drive H is Local Disk
Volume Serial Number is 686B-F4CF

Directory of H:\JavaProjects\HotImage\HotImage\classes\hotimage

06/04/2004 01:06p <DIR> .
06/04/2004 01:06p <DIR> ..
28/03/2002 11:29p 78,727 daynite.gif
28/03/2002 11:18p 7,667 deerani.gif
31/03/2004 04:07p 4,757 HotSpotImageTest.class
06/04/2004 01:06p 103,281 HotSpotImageTest.jar
31/03/2004 04:07p 683
HotSpotImageTest_jLabel1_mouseAdapter.class
31/03/2004 04:07p 935
HotSpotImageTest_jLabel2_mouseAdapter.class
06/04/2004 01:04p 30 manifest.mf
21/06/2003 04:42a 100,096 sunset.jpg
8 File(s) 296,176 bytes
2 Dir(s) 17,338,773,504 bytes free

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest
'HotSpotImageTest' is not recognized as an internal or external command,
operable program or batch file.

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest.jar

H:\JavaProjects\HotImage\HotImage\classes\hotimage >jar cvfm
HotSpotImageTest.jar manifest.
mf *.class *.jpg
added manifest
adding: HotSpotImageTest.class(in = 4757) (out= 2304)(deflated 51%)
adding: HotSpotImageTest_jLabel1_mouseAdapter.class(in = 683) (out=
358)(deflated 47%)
adding: HotSpotImageTest_jLabel2_mouseAdapter.class(in = 935) (out=
413)(deflated 55%)
adding: sunset.jpg(in = 100096) (out= 99228)(deflated 0%)

H:\JavaProjects\HotImage\HotImage\classes\hotimage >HotSpotImageTest.jar

H:\JavaProjects\HotImage\HotImage\classes\hotimage >


Ted,

It may be that *.jar files are not properly configured on your Windows
machine. (Sometimes other programs, like zip programs, hijack *.jar
extensions for themselves.) Try this:

java -jar HotSpotImageTest.jar

If java is not found, enter the full path to java.exe, e.g.

C:\j2sdk1.4.2_03\bin\java.exe -jar HotSpotImageTest.jar

Now you should at least get some error messages.

Ray
Jul 17 '05 #2

P: n/a

"Raymond DeCampo" <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in
message news:%k***************@twister.nyroc.rr.com...
Ted Byers wrote:


Ted,

It may be that *.jar files are not properly configured on your Windows
machine. (Sometimes other programs, like zip programs, hijack *.jar
extensions for themselves.) Try this:

java -jar HotSpotImageTest.jar

If java is not found, enter the full path to java.exe, e.g.

C:\j2sdk1.4.2_03\bin\java.exe -jar HotSpotImageTest.jar

Now you should at least get some error messages.

Thanks Ray

Yes, now I get an error message. The following one:

H:\JavaProjects\HotImage\HotImage\classes\hotimage >java -jar
HotSpotImageTest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: HotSpotImageTest
(wrong name: hotimage/HotSpotImageTest)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :502)
at
java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:250)
at java.net.URLClassLoader.access$100(URLClassLoader. java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 99)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 55)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:315)

Here's the contents of my directory:
31/03/2004 04:07p 4,757 HotSpotImageTest.class
06/04/2004 01:06p 103,281 HotSpotImageTest.jar
31/03/2004 04:07p 683
HotSpotImageTest_jLabel1_mouseAdapter.class
31/03/2004 04:07p 935
HotSpotImageTest_jLabel2_mouseAdapter.class
06/04/2004 01:04p 30 manifest.mf
21/06/2003 04:42a 100,096 sunset.jpg

This is in manifest.mf:
Main-Class: HotSpotImageTest
And here are the first few lines of HotSpotImageTest.java:

package hotimage;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @Ted Byers
* @version 1.0
*/

public class HotSpotImageTest extends JApplet {
....

Have I overlooked something here, or is my installation of the SDK broken?
You DID say "It may be that *.jar files are not properly configured on your
Windows machine. (Sometimes other programs, like zip programs, hijack *.jar
extensions for themselves.)" If this is broken, how do I fix it, and is the
cause of the exception I got when I tried "java -jar HotSpotImageTest.jar"?

Here is another, unrelated question. What is the "package hotimage;" for?
It was added, along with the other first half dozen lines, by JBuilder, and
it isn't the sort of thing I normally think about when working on a little
project. Is it really necessary, or can it be removed without risk?

Thanks,

Ted
Jul 17 '05 #3

P: n/a
Ted Byers wrote:

Yes, now I get an error message. The following one:

H:\JavaProjects\HotImage\HotImage\classes\hotimage >java -jar
HotSpotImageTest.jar
Exception in thread "main" java.lang.NoClassDefFoundError: HotSpotImageTest
(wrong name: hotimage/HotSpotImageTest)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :502)
at
java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:250)
at java.net.URLClassLoader.access$100(URLClassLoader. java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 99)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 55)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:315)

[snip]

Have I overlooked something here, or is my installation of the SDK broken?
You DID say "It may be that *.jar files are not properly configured on your
Windows machine. (Sometimes other programs, like zip programs, hijack *.jar
extensions for themselves.)" If this is broken, how do I fix it, and is the
cause of the exception I got when I tried "java -jar HotSpotImageTest.jar"?

Here is another, unrelated question. What is the "package hotimage;" for?
It was added, along with the other first half dozen lines, by JBuilder, and
it isn't the sort of thing I normally think about when working on a little
project. Is it really necessary, or can it be removed without risk?


Aha, this is indeed the source of your problem. You could get around it
by getting rid of the package declaration, but you will be better off in
the long run if you keep it and fix your problem.

Here's how to fix the problem. First, in the MANIFEST.MF the Main-Class
attribute must be the fully-qualified name of the class. In this case
the FQN is "hotimage.HotSpotImageTest".

Second, make sure you have the jar file packaged correctly. The
HotSpotImageTest.class file should be in a hotimage directory in the jar
file. Test this by entering in "jar tvf xyz.jar
hotimage/HotSpotImageTest.class". You should see some details about the
file within the jar.

Now you should be good to go, using the command as before.
Now, why use packages and what do they do? Packages provide a name
space for your classes. The Java standard is to use the reverse of your
domain name plus whatever you want for your packages. For example, if
your company owns xyz.com, use com.xyz. This way you will not meet any
other classes from other libraries with the same name as your class.

When the JVM attempts to load a class with a package declaration, it
turns the '.' characters into '/' characters, appends the ".class".
Then each location in the CLASSPATH is treated as the base until one of
the locations contains the class. For example, if the CLASSPATH is
"my/classes;abc.jar" and the JVM tries to load com.xyz.Main, it will
look for "my/classes/com/xyz/Main.class". Failing that, it will look
for "com/xyz/Main.class" within the abc.jar jar file.

(Disclaimer: the above paragraph assumes you are using the default
UrlClassLoader. If you are using a different ClassLoader, different
things will happen.)

Note that when you run the JVM using the -jar option, the jar file is
prepended to the CLASSPATH, the remainder of which comes from the
Class-Path entry in the META-INF/MANIFEST.MF, if it exists.

Finally, there are some other reasons to use packages. When you make an
instance variable or a method with no access specifier (i.e. private,
protected or public), that variable or method has package level access.
That means any other class in the same package can see the variable or
method. This is similar to the C++ "friends" concept.
OK, you asked about associating jar files on Windows. To manage file
associations on Windows, open the Explorer. Go to Tools->Options and
select the File Types tab. (This may be different in the various
flavors of Windows.) You should see a list of file types. Find the JAR
file type. You need to change the default application associated with
JAR files to be javaw and pass the -jar option. (Actually, you may not
need to do this. Since javaw is a true Windows program, it does not
have a console and so it could not emit error messages. So the behavior
you saw before is consistent with that.)

HTH,
Ray

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.