File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes IDEs, Version Control and other tools and the fly likes How to convert java to jar. jar to batch file in eclipse IDE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "How to convert java to jar. jar to batch file in eclipse IDE" Watch "How to convert java to jar. jar to batch file in eclipse IDE" New topic

How to convert java to jar. jar to batch file in eclipse IDE

sulthan mathina

Joined: Jan 31, 2011
Posts: 6
Hi all,
I have created java application with properties file and sql server database. It running successfully in eclipse.Then i convert it into jar by new-->export-->...........etc. Created. Now i want to create batch file for this jar. I created run.bat but i did not get my output.
I run jar in cmd by java -jar sss.jar
It throwing exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logg
at smapp.Main.<clinit>(
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at$ Source)
at Method)
at Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Could not find the main class: smapp.Main. Program will exit.

Peter Johnson

Joined: May 14, 2008
Posts: 5852

Looks like you are using Log4J in you app - did you include that JAR file in your classpath? (It would help if you posted the contents of you bat file).

Which class did you identify as the "main" class? The JVM thinks that it is smapp.Main - do you have such a class? (It would help if you posted the manifest from your JAR file.)

Also, post the contents of your JAR by running:

jar -tf sss.jar

JBoss In Action
sulthan mathina

Joined: Jan 31, 2011
Posts: 6
hi ,

C:\>jar -tf ss.jar

bat file

set $PATH=$PATH;"C:\Program Files\Java\jdk1.6.0_20\bin"

java -jar ss.jar

echo Completed Successfully....
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

Like Peter mentioned, you need to specify your main class in the manifest and ensure the log4j jar is on the classpath
Recommended reading

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Stuff like this is hard to do using just Eclipse, because Eclipse is a development environment, not a runtime environment and Java's classpath rules require that you do some things that are complicated enough that a "one-size-fits-all" Eclipse build doesn't handle.

You can do one of 2 things:

1. You can create a batch file with an explicit classpath reference for the additional jar(s) such as log4j. Needless to say, the deployment of something like this gets complicated, since now you have a Java app jar, a log4j library jar and an MS-DOS batch file to install on the user's system instead of just a simple file copy.

2. You can build an executable JAR. This solution provides a single file, which can be executed using the "java -jar ...." command line. However, in order to resolve the log4j classes, you'll have to do something more. Either a) explode the log4j car and merge its classes into your application jar, or b) add a custom classloader to your app so that you can include the unexploded log4j.jar embedded in your executable jar.

The reason for the extra complexity is that jar-in-jar organization isn't supported by the default behaviour of the JVM, so you have to include your own classloader to locate and load classes from the embedded jar(s). However, this is more work than most of us want to go through. Fortunately, if you use Maven, you can invoke the "build executable jar" goal and it will do all that for you.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: How to convert java to jar. jar to batch file in eclipse IDE
It's not a secret anymore!