jar file won't run

M

michau

Hi, I have a project in NetBeans, the project compiles, no errors, I
also included in it all required extra libraries:
javamail 1.4.1,
jaf (Java Activation Framework) 1.1.1
(except for the standard Java ones)

I have two main classes, that I can both run from NetBeans.
However if I use "Build Project" in NetBeans (which gives me a
ProjectName.jar file), I cannot run the jar file. There's an error
message after trying to run it:
"The jar file "ProjectName.jar" couldn't be launched. Check the
Console for possible error messages"
Also if I try to use jar command from a terminal (on Mac OS) to create
a .jar file for one of the two main classes, like so:
"jar cf mainclass1.jar *.class"
it gives me the same error.

Where can I finde the "console", what is it, can the solution to the
problem be there, or is it some other, perhaps trivial, issue?

Help ?

Thanks in advance,
Michal
 
L

Lew

michau said:
Hi, I have a project in NetBeans, the project compiles, no errors, I
also included in it all required extra libraries:
javamail 1.4.1,
jaf (Java Activation Framework) 1.1.1
(except for the standard Java ones)

I have two main classes, that I can both run from NetBeans.
However if I use "Build Project" in NetBeans (which gives me a
ProjectName.jar file), I cannot run the jar file. There's an error
message after trying to run it:
"The jar file "ProjectName.jar" couldn't be launched. Check the
Console for possible error messages"
Also if I try to use jar command from a terminal (on Mac OS) to create
a .jar file for one of the two main classes, like so:
"jar cf mainclass1.jar *.class"
it gives me the same error.

Where can I finde the "console", what is it, can the solution to the
problem be there, or is it some other, perhaps trivial, issue?

In NetBeans the console is one of the windows in the view - where all the
output messages appear. From the command line the console is the command window.

When you invoke the JAR from your command line, what is the command?
 
M

michau

Hi, I invoke by clicking it.


In NetBeans the console is one of the windows in the view - where all the
output messages appear.  From the command line the console is the command window.

When you invoke the JAR from your command line, what is the command?
 
L

Lew

michau said:
Hi, I invoke by clicking it.

Please do not top-post. Use trim-and-inline posting, as here.

How do you have the association set up for the (double?) click in the OS?
IOW, what command does the (double?) click invoke on the JAR file?
 
M

Mike Schilling

Lew said:
Please do not top-post. Use trim-and-inline posting, as here.

How do you have the association set up for the (double?) click in
the
OS? IOW, what command does the (double?) click invoke on the JAR
file?

And what's in the jar's manifest?
 
R

Roedy Green

Hi, I have a project in NetBeans,

Netbeans complicates matters. Temporarily remove it from
consideration.

Examine your jar with Jarlook see
http://mindprod.com/jgloss/products1.html#JARLOOK
too make sure all the classes you need are in there.

Run some OTHER known good jar to make sure your association to
java.exe si working. See http://mindprod.com/jgloss/association.html

Run your jar from the command line.

You now know if the problem is with your jar, or your OS. If they are
both fine, the problem is configuring netbeans to find your jar.
 
M

michau

OK. I invoked my .jar file (ladder.jar) from the Terminal using
command "java -jar ladder.jar"
The message said:
Failed to load Main-Class manifest attribute from
ladder.jar

I'm not an expert but, does it mean that I should have input a line
declaring a main class for the .jar file somewhere in some manifest
file?

Thanks,
Michal
 
M

Mike Schilling

michau said:
OK. I invoked my .jar file (ladder.jar) from the Terminal using
command "java -jar ladder.jar"
The message said:
Failed to load Main-Class manifest attribute from
ladder.jar

I'm not an expert but, does it mean that I should have input a line
declaring a main class for the .jar file somewhere in some manifest
file?

Got it in one. Here's a starting point for learning about manifests.
http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
 
K

Karl

michau said:
OK. I invoked my .jar file (ladder.jar) from the Terminal using
command "java -jar ladder.jar"
The message said:
Failed to load Main-Class manifest attribute from
ladder.jar

I'm not an expert but, does it mean that I should have input a line
declaring a main class for the .jar file somewhere in some manifest
file?

If you created the project in NetBeans, it should create an executable jar
with the correct entries in the manifest. NetBeans (5.51 anyway) prints out
this message when you build the project:

To run this application from the command line without Ant, try:
java -jar "C:\Documents and
Settings\YourName\ProjectName\dist\ProjectName.jar"

However, it is possible that you made changes to the project that NetBeans
did not detect, rendering it defective.
 
N

Nigel Wade

michau said:
Hi, I have a project in NetBeans, the project compiles, no errors, I
also included in it all required extra libraries:
javamail 1.4.1,
jaf (Java Activation Framework) 1.1.1
(except for the standard Java ones)

I have two main classes, that I can both run from NetBeans.
However if I use "Build Project" in NetBeans (which gives me a
ProjectName.jar file), I cannot run the jar file. There's an error
message after trying to run it:
"The jar file "ProjectName.jar" couldn't be launched. Check the
Console for possible error messages"

What type of project did you create in NetBeans? For NetBeans to create the
correct manifest, and to put the dependent jars where the application jar can
locate them, the project must be a Java Application. It also requires that the
project have a main class set in its properties.

If you do this then NetBeans should copy any dependent jars into the dist/lib
directory of the project. The manifest of the application jar should point to
the jars in the dist/lib directory, and so the application jar can be run
without any special command line arguments.

There is one exception to this, burried in the help:
"If you added a folder of classes or resources to the classpath (as opposed to
a JAR file or project), none of the classpath elements are copied to the dist
folder."

So if you added the javamail or jaf libraries as a folder rather than a jar then
the dist/lib directory may not have been created. Check under the project's
directory structure to see whether dist/lib has been created and populated with
the javamail and jaf jars.
Also if I try to use jar command from a terminal (on Mac OS) to create
a .jar file for one of the two main classes, like so:
"jar cf mainclass1.jar *.class"
it gives me the same error.

To build the application jar you need to tell the compiler where the dependent
jars are located (javamail and jaf). You do this using the -classpath argument.
Where can I finde the "console", what is it, can the solution to the
problem be there, or is it some other, perhaps trivial, issue?

The location of the console will depend on your desktop. There may not be one.
All it will show you is any error message generated by the command that
activating the jar from the desktop generated. You can do the same for youself
from a command shell. Change directory to the dist directory for the project
and run the command 'java -jar ProjectName.jar'.
 
M

Mark Space

michau said:
I'm not an expert but, does it mean that I should have input a line
declaring a main class for the .jar file somewhere in some manifest
file?

Karl posted a good link for manifest files, and Nigel also brought up a
good point. NetBeas won't create a main class if you are building a
library, as opposed to an application. I don't know there a way to
switch project types. In a pinch, you could copy just the sources to a
new subdirectory, and then use Create a New Project from Existing Sources.

(That's Existing Sources, not Existing Project or With Existing Ant
Build Script. The latter would almost certain pick up on the lack of a
main class and perpetuate it.
 
N

Nigel Wade

Mark said:
Karl posted a good link for manifest files, and Nigel also brought up a
good point. NetBeas won't create a main class if you are building a
library, as opposed to an application.

It's not the main class that's the real problem, you can always add that
yourself and set it in the project properties. If the project isn't a Java
Application the ant script which NetBeans auto generates doesn't create the
dist/lib directory and copy the dependent jars into it, nor does it create a
manifest pointing to the dependent jars. That means that the project jar can't
be run directly outside of NetBeans. It requires a classpath to tell it where
to locate the jars it needs.
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top