views:

242

answers:

3

i have a simple Demo.java file in D:\jarConcepts directory:

import javax.swing.* ;

class Demo{
    public static void main(String args[]){
     JFrame frame = new JFrame("") ;
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ;

     Class c = null ;
     try{
      c = Class.forName("com.mysql.jdbc.Driver") ;

      //com.mysql.jdbc.Driver class is in a jar file
      //whose class path is set in the environment variable(explicitly done by me)

      //when i am executing Demo.class using java command, it displays frame with OK title
      //but if i execute this by creating a jar, it enables to load the
      //com.mysql.jdbc.Driver class,
      //thus displaying frame with Sorry title

      frame.setTitle("OK") ;
     }
     catch(ClassNotFoundException cnfe){
      frame.setTitle("Sorry") ; 
     }

     frame.setVisible(true) ;
    }
}

I prepared a manifest.txt file in D:\jarConcepts with following text :

Main-Class: Demo

Class-Path: C:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jar

when, i create a jar file from the same directory using

jar -cvfm Demo.jar manifest.txt .class

following is the output :

added manifest adding: Demo.class(in = 743) (out= 505)(deflated 32%)

But, when i execute the jar file generated, it shows an error message,

Could not find the main class. Program will exit.

i don't understand why this is happening, coz, when i creating the jar file with the following manifest code :

Main-Class: Demo

i am getting a perfectly executable Demo.jar, the only problem is that it is not loading the Driver class from the ] class path and when i am trying to add the path in the manifest, it's not working...... plz help.......

A: 

Ensure that there is not a newline in your manifest file between the Main-Class and Class-Path entry. You should also ensure that there is a newline after the Class-Path entry.

Also I would recommened that Demo be a public class if it is to be used as the main class.

Mark
then why the jar file is executing, when i am only using the Main-Class tag instead of using bothMain-Class, Class-Path tags......
rits
A: 

You should not rely on the manifest classpath, as the Manifest files has some pretty strange rules, including the ones for line wrapping.

Instead, build a classpath using command line arguments and invoke your program using the main class argument java -cp Demo.jar:mysql-connector.jar Demo

Robert Munteanu
that's a long list for different purposes....will u plz tell me the only text which i have to write in manifest.txtto make my frame visible with title OK....
rits
As I said, I can only suggest building the classpath with 'java -cp', not with the Manifest.
Robert Munteanu
mr. robert, thnx for your response.....i have 1 more question to u,i am new on this site.will u plz tell me the use oflink link(on left of flag link) on this page...???
rits
+1  A: 

The spaces are interpreted as delimiters and the entries should be relative:

http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Main%20Attributes

Class-Path :

The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs. URLs are separated by one or more spaces.

Bert F
so what url i have to give if my jar is inC:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jardirectory....
rits
You should copy the jar to a location relative to your jar (e.g. lib/mysql-connector-java-5.0.4-bin.jar) - in theory, you would have to do something like this when you create your deployment package for your app. In a pinch, you could also try "file:///C:/Program%20Files/MySQL/CMySQL%20Tools%20for%205.0/java/lib/mysql-connector-java-5.0.4-bin.jar".
Bert F
Thinking of packaging - I've used ant to merge the dependency jars into my jar (unjarring the dependency jars and jar everything together). I think Sun really dropped the ball by not providing better support for embedding dependency jars with the application jar.However, using a custom classloader seems like less of a hack: http://www.ibm.com/developerworks/library/j-onejar/
Bert F