mogli
mogli

Reputation: 1609

adding jars to your program

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.......

Upvotes: 0

Views: 660

Answers (3)

Mark
Mark

Reputation: 29139

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.

Upvotes: 0

Bert F
Bert F

Reputation: 87573

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.

Upvotes: 1

Robert Munteanu
Robert Munteanu

Reputation: 68308

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

Upvotes: 0

Related Questions