wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes self executable jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "self executable jar" Watch "self executable jar" New topic
Author

self executable jar

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,
I am trying to create a self executable jar.
I have a project in eclipse. Its a non web java project.
I have created a jar of this project using eclipse.

When I try to run this jar by double clicking on it, it fails.
Giving the following error.
"Could not find the main class. Program will exit."

The same jar runs when I give a command from the command prompt.
from C: or D: drive.

I want this jar to run when I double click on it.

Any suggestions.

Priety.


Priety.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

"Priety Cool",

Please read your private messages regarding an important message.


As for your problem, you need to specify the main class in your manifest file. This looks like the following:

This will tell the JVM what class it should actually execute; it can't just go searching all classes and pick one with a main method - what if there are more than one classes?

Execute "jar" in a command window without any arguments to see all possible options for JAR.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

For more detailed information on how to package programs in JAR files, see: The Java Tutorials - Packaging Programs in JAR Files


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Rob,
Thanks a lot.

The manifest file generated by Eclipse has Main-Class attribute.
And the attribute mentions the the name of he class as desired.

But when I try to run this from the command prompt now using the following command, I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

My jar is on the D: drive.
I give the following command from there.
D:\>java -jar Test.jar

I have the log4j.jar file in my classpath.
When I do:
D:\>echo %classpath%
D:/log4j-1.2.15.jar; is seen as a part of the classpath.


My classpath goes as follows :
.;<jarfilename.jar>;jarfilename.jar>;

It includes all the jar files used by my project.

How do I proceed further and get rid of this error.

Priety.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

JAR files ignore your usual classpath. You will have to add the Log4J library to the Class-Path directive.

For instance, put the Log4J JAR file in a folder relative to the JAR file called lib, then the class path should be

You can add multiple libraries to the class path. Check Jesper's link for more info.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Rob,

Thanks a lot for this solution.
It works super.
Its a relief to get thing working.

Thanks again.
Priety.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Will you now read your private messages?
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Rob,

I have read the message.

Is it ok now? I have changed my last name.

Priety.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

I could get that self executable jar working yesterday.
But now today after restaring my computer and launching the eclipse ide, problems again.

Unable to find log4j jar.
Really strange.
When I create the jar using the preexisting manifest file in eclipse, it breaks the class-path value and probably thus making it unreadable from the manifest file.
Is this a problem with eclipse?

Is there any solution for this.
I try to run this jar from the D rive from the command prompt.
And again get classnotfound error for the Logger classes.



Priety.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: self executable jar