Executing a Jar file created using netbeans, Without opening Netbeans IDE

dtivmk
dtivmk used Ask the Experts™
on
I have created a project using netbeans. It runs perfect when run from netbeans, i.e by right clicking on the project folder, doing a clean and build and then clicking run. The jar file get created into the dist folder.
When i double click on the jar file, it says: Could not find the main class. Program will exit.
How can i run this JAR file without using netbeans ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you will need to add a manifest file to be able to run jar by doubleclicking on it!
check this example to modify the build.xml to add the manifiest while creating jar!

http://www.rgagnon.com/javadetails/java-0532.html

Author

Commented:
I think the manifest file is already included as when open the jar file in netbeans, i can see it contains a manifest file, a class file and a dat file.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

check if it has Main-Class: entry!
Commented:
Make sure that the Main Class under project properties has been set.
Netbeans then adds the manifest file with the Main Class information into the jar.

Check if your jar file contains the manifest file in the META-INF directory.
If so, check if the main class is set e.g.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 10.0-b22 (Sun Microsystems Inc.)
Main-Class: com.yourpackage.yourMainClass
Class-Path: lib/appframework-1.0.3.jar lib/swing-worker-1.1.jar lib
X-COMMENT: Main-Class will be added automatically by build

Author

Commented:
the manifest file looks like this :

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_11-b03 (Sun Microsystems Inc.)
Main-Class: <class-name>

Author

Commented:
there is no class path and  a com.package.
Do i have to change something from project properties ?
follow the link I have posted!
<target name="jar" depends="compile">
     <delete file="hello.jar"/>
     <delete file="MANIFEST.MF"/>
     <manifest file="MANIFEST.MF">
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Main-Class" value="howto.Hello"/>
    </manifest>
 
      <jar destfile="hello.jar"
           basedir="."
           includes="**/*.class"
           manifest="MANIFEST.MF"
           />
  </target>

Author

Commented:
below is the code of my build.xml. Where do i add the code that u've mentioned. I tried adding into this, but there was no change.
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="Msg_producer" default="default" basedir=".">
    <description>Builds, tests, and runs the project Msg_producer.</description>
    <import file="nbproject/build-impl.xml"/>
    <!--
 
    There exist several targets which are by default empty and which can be 
    used for execution of your tasks. These targets are usually executed 
    before and after some main targets. They are: 
 
      -pre-init:                 called before initialization of project properties
      -post-init:                called after initialization of project properties
      -pre-compile:              called before javac compilation
      -post-compile:             called after javac compilation
      -pre-compile-single:       called before javac compilation of single file
      -post-compile-single:      called after javac compilation of single file
      -pre-compile-test:         called before javac compilation of JUnit tests
      -post-compile-test:        called after javac compilation of JUnit tests
      -pre-compile-test-single:  called before javac compilation of single JUnit test
      -post-compile-test-single: called after javac compilation of single JUunit test
      -pre-jar:                  called before JAR building
      -post-jar:                 called after JAR building
      -post-clean:               called after cleaning build products
 
    (Targets beginning with '-' are not intended to be called on their own.)
 
    Example of inserting an obfuscator after compilation could look like this:
 
        <target name="-post-compile">
            <obfuscate>
                <fileset dir="${build.classes.dir}"/>
            </obfuscate>
        </target>
 
    For list of available properties check the imported 
    nbproject/build-impl.xml file. 
 
 
    Another way to customize the build is by overriding existing main targets.
    The targets of interest are: 
 
      -init-macrodef-javac:     defines macro for javac compilation
      -init-macrodef-junit:     defines macro for junit execution
      -init-macrodef-debug:     defines macro for class debugging
      -init-macrodef-java:      defines macro for class execution
      -do-jar-with-manifest:    JAR building (if you are using a manifest)
      -do-jar-without-manifest: JAR building (if you are not using a manifest)
      run:                      execution of project 
      -javadoc-build:           Javadoc generation
      test-report:              JUnit report generation
 
    An example of overriding the target for project execution could look like this:
 
        <target name="run" depends="Msg_producer-impl.jar">
            <exec dir="bin" executable="launcher.exe">
                <arg file="${dist.jar}"/>
            </exec>
        </target>
 
    Notice that the overridden target depends on the jar target and not only on 
    the compile target as the regular run target does. Again, for a list of available 
    properties which you can use, check the target you are overriding in the
    nbproject/build-impl.xml file. 
 
    -->
  
</project>

Open in new window

Author

Commented:
My jar file consists of :
1. Main.class
2. application-client (XML File)
3 .MANIFEST.MF

the Manifest looks like:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 10.0-b22 (Sun Microsystems Inc.)
X-COMMENT: Main-Class will be added automatically by build
Main-Class: msg_producer.Main

Commented:
you could add ksivananth jar code by adding it with the empty target -pre-jar.
<target name="-pre-jar">
     <delete file="hello.jar"/>
     <delete file="MANIFEST.MF"/>
     <manifest file="MANIFEST.MF">
        <attribute name="Built-By" value="${user.name}"/>
        <attribute name="Main-Class" value="howto.Hello"/>
    </manifest>
 
      <jar destfile="hello.jar"
           basedir="."
           includes="**/*.class"
           manifest="MANIFEST.MF"
           />
  </target>

Open in new window

Author

Commented:
Adding this code to the build.xml  is not making any difference. For example, here i say destfile="hello.jar", It still makes Msg_producer.jar. I may have mis-understood your explaination. Pls guide in detail.I'm a complete beginner. Thanks !

Author

Commented:
When i put the Msg_jar file into "C:\Program Files\Java\jre1.6.0_06\lib>" and then run " javaw -jar Msg_producer.jar" from command line, i get an alert which says : "Unable to access jar file Msg_producer.jar".
And when i copy this jar file into "C:\Program Files\Java\jdk1.6.0_06\bin>" and then run it similarly, it says: " could not find the main class.Program will exit".
Commented:
Ok. I tried it in my Netbeans project.
Here is my build.xml file from netbeans.
I removed the comments and marked the important part for you.
<?xml version="1.0" encoding="UTF-8"?>
<project name="My-Test-Project" default="default" basedir=".">
    <description>Builds, tests, and runs the project Test-Project.</description>
    <import file="nbproject/build-impl.xml"/>
    
<!-- ###################################### -->
<!-- Important part for your build.xml file -->
    <target name="-pre-jar">
        <delete file="MANIFEST.MF"/>
        <manifest file="MANIFEST.MF">
            <attribute name="Built-By" value="me"/>
            <attribute name="Main-Class" value="msg_producer.Main"/>
        </manifest>
    </target>
<!-- End of important part for your build.xml file -->
<!-- ###################################### -->
 
</project>

Open in new window

Commented:
You do not need to delete any jar or manifest file because the target is executed before the jar will be created. Just make a clean & build and check the manifest file again.

There is no need to put your jar file in the Java directory.

Is your Main.class located under the directory msg_producer in your jar?
Is your MANIFEST.MF file located under META-INF directory in your jar?

Author

Commented:
I copied your code into my build.xml file. Then, did a clean and build. Opened my Jar. It has a Msg_producer folder that contains the Main.class and it contains a META-INF folder that contains MANIFEST.MF and application-client.XML.
Next, I opened CMD, went in to directory of Netbeans projects > Msg_producer > dist
Gave the command: javaw -jar Msg_producer.jar

It gives an alert msg: "could not find main class.Program will now exit"
Commented:
You should use
java -jar Msg_producer.jar

Your Main.class contains the method:

public static void main(String[] args) {
       // call your Main class and do your things...
        Main mainTest = new Main();
        // maybe you are calling another method ? ...
}

 right?
Maybe you could obfuscate your code and show us your Main source?

What I have done is
1. Created a new project named Msg_producer
2. Created a Main class that is located in the Source Packages under "msg_producer"
3. Made an instance of the Main class in my main method
4. Finally build the project.
5. Opened a console and stepped into the folder where my project is located, then into the dist folder
6. typed: java -jar Msg_producer.jar and it worked.

javaw -jar Msg_producer.jar did not print anything on my console - javaw is without output.

The text below is only useful when your main class problem is fixed.
You could change the start type of java programs from javaw to java by following these steps posted on this site -> http://forums.sun.com/thread.jspa?messageID=687991

"Open a Windows folder. On the Tools menu, select Folder Options.
Click on File Types tab and find the JAR file type. Click on it, then click on Advanced.
Click on "open" then click on Edit.
Set the Application Path as follows making appropriate changes to match where your Java is installed:
"C:\Program Files\Java\jre1.6.0_01\bin\java.exe" -jar "%1" %*
Then click OK until everything closes."

Author

Commented:
I ran the jar file using: " java -jar Msg_producer.jar" and this is what i got:
 

Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/JMSExceptio
n
Caused by: java.lang.ClassNotFoundException: javax.jms.JMSException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Commented:
You have to add the jms.jar to your project.
And then the lib should be added in your dist folder.

Your manifest.mf file in the project folder should look like this


Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Open in new window

Author

Commented:
Yes my MANIFEST files looks the same as above. I added the jms.jar file into the project folder. It was in the Glassfish>imq>lib folder. Still gives the same result. Could there be a class path problem ? Can you tell, how i can check the class path and let you know ? Also , u've mentioned that the lib will be added into my dist folder. Do i have to do this manually? if yes, then where do i find this lib??
Commented:
You can set a lib folder when you enter the project properties and choosing the Libraries Categories on the left. Below the Java Platform Combobox you can choose the Libraries Folder with the Browse button.
You can leave the default ./lib value and click next and finish. Now you can add your libraries to the lib folder. After this you should clean & build again and netbeans should realize the new lib folder and copy the libs into dist.
This fixes the NoClassDefFoundError. Then try executing your jar again and post the result.

Author

Commented:
If the libraries are not added, how does it run when running from netbeans ?

Commented:
I do not exactly know how netbeans a program starts. But netbeans does not call the builded jar in the dist folder. It also uses the netbeans classpath where the libs are known.

Author

Commented:
Okay. I built a new java desktop application using netbeans. Did a build and generated a jar file. It works perfectly fine. Even on double clicking the jar file, the program execution starts. I checked its dist folder, and it had a lib folder which contains jar files. This means that the process is correct. Also, I did not add any extra lib files before building it. It was by default, what netbeans selects. Now, I understand that there  is surely a problem with the lib files in Msg_producer application, as I dont see any lib folder in the dist folder of Msg_producer. The only difference between Msg_producer and the working java application is that, the working application is a J2SE project, while Msg_producer is J2EE project.

Author

Commented:
I made the J2EE application from scratch, and at each step i checked whether the jar file generated gets executed on a double click. It worked, until i inserted the below given code. Could you tell me what the cause could be? As soon as i inserted this code, it gave the message : "Could not Find the main class. Program will exit"
Its a JMS application with Glassfish application server.
try 
        {
            if (destType.equals("queue")) 
            {
                dest = (Destination) queue;
            } 
            else 
            {
                dest = (Destination) topic;
            }
        } 
        
        catch (Exception e) 
        {
            System.err.println("Error setting destination: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

Open in new window

Author

Commented:
Ok. The problem is sorted out. I was making a J2EE JMS application that required three jar files to run:
imq.jar
javax.jms.jar
jms.jar
All these files had to be added to the CLASSPATH. I did this by Copying these files to the jre/lib/ext folder.
The Program now, executes by double clicking on the JAR file.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial