0

I have created a simple database in MYSQL, which I am trying to pull data from in a java program written in Eclipse. When I run the program in eclipse, it pulls out the data fine, but when I export the program into a runnable JAR, it just won't work!!

I have addedthe mysql connector by using the "add External Archive" feature in the build path, and have checked that its there by using

System.out.println("classpath = " + System.getProperty("java.class.path"));

which gives the results: classpath = C:\Users\Andy\workspace\test2\bin;C:\Users\Admin\Desktop\mysql-connector-java-5.1.16\mysql-connector-java-5.1.16-bin.jar

I have also opened up the exported JAR file and have seen that the connector has been added.

Here is the code that I am using:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import javax.swing.JOptionPane;



public class test
{
    public static void main(String args[])
{
    try
    {

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection dbConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cambridge","root","P@ssw0rd");
        System.out.println("classpath = " + System.getProperty("java.class.path"));

        Statement stmt = dbConn.createStatement();

        String strSQL = "Select * from customer_details";

        ResultSet rs = stmt.executeQuery(strSQL);

        ResultSetMetaData rsmd = rs.getMetaData(); 

        int nCol = rsmd.getColumnCount();

        while(rs.next())
        {
            for(int col=1; col<=nCol; col++)  
            {
                System.out.print(rs.getString(col));
                JOptionPane.showMessageDialog(null, rs.getString(col));
            }

            System.out.println();
        }

        System.out.println();

        dbConn.close();  
    }
    catch (Exception excp)
    {
        excp.printStackTrace();
    }
}
}

Thanks in advance for any help you can offer!!!

Andy
  • 23
  • 2
  • 5
  • What do you mean by "it just won't work!!"? Surely, a stack trace would have been produced in the event of a failure. – Vineet Reynolds Jun 06 '11 at 03:58
  • what is the exception that you are seeing? – Edwin Dalorzo Jun 06 '11 at 03:58
  • No, nothing comes out. I try to open the JAR file, it thinks about it for a second (the circle icon shows up) then nothing. I have tried a different set of code where I connect to the database after pushing a button, the program loads, but again, when you press the button, nothing happens!! – Andy Jun 06 '11 at 04:01
  • Sorry, there is no exception showing. Literally, nothig happens. I've tried doing research into this, and everyone who has a similar problem all mention making sure the classpath is correct. I'm pretty sure I have done this part right, so I have no idea what to try now!! – Andy Jun 06 '11 at 04:09
  • Have you tried removing the JOptionPane and running this from a console? – Vineet Reynolds Jun 06 '11 at 04:15
  • I have tried running it without the JOptionPane, but not from the console. Not entirely sure how to do that....am quite new to java and programming! – Andy Jun 06 '11 at 04:23
  • Just remove the statements that use JOptionPane - the import statement and the statement in the for loop. – Vineet Reynolds Jun 06 '11 at 04:30
  • @Andy, if you are getting a problem similar to the one posted in [this problem](http://stackoverflow.com/questions/5416214/classnotfoundexception-after-exporting-from-working-application-in-eclipse), it is an issue with the classpath; you're better off including the location of the mysql connector JAR in the Manifest of the JAR. Any other problem would require further investigation. – Vineet Reynolds Jun 06 '11 at 04:46
  • When you have the .jar file you can run it by opening command prompt and execute: java -jar – Danail Nachev Jun 06 '11 at 09:48
  • Ok, so I've just run the program from the command line (thanks Danail) and the program works fine!! Connects to the database, and pulls all the information fine, but again, when I run the program by just double clicking on it, it does't connect!?!? Any ideas as to why, and what I need to do to fix this?? – Andy Jun 06 '11 at 16:40
  • @Vineet I have recoded so I can output the error message, and I got a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver So I think it is a class problem like the one you pointed to, but I've still no idea how I can fix it. Would you or anyone else be able to walk me through what I would need to do? Many thanks – Andy Jun 07 '11 at 02:43
  • @Andy, I suppose you are not specifying the classpath when starting the application. The `CLASSPATH` variable is ignored if you start the application as `java -jar ...`. Use `java -classpath myjar.jar;anotherjar.jar... MainClass` to start instead. I'll refer you to the [Java application launcher page](http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html) – Vineet Reynolds Jun 07 '11 at 02:50
  • Is that not for the cmd line though? What would I need to change so when I click on the jar file it would find the classpath? – Andy Jun 07 '11 at 03:30

1 Answers1

0

FINALLY GOT IT!!!

I did the following:

Window -> Preferences -> Java -> Build Path -> Classpath Variables

then clicked on new, entered a name, then clicked on file and navigated to the mysql connector, and then hit ok!!

Not entirely sure why this worked, but when I did pretty much the same, but for an individual project didn't, but it worked, and thats the main thing!!

Thanks to everyone who responded and offered me some help. It took me a while, but I got there in the end!! :o)

Andy
  • 23
  • 2
  • 5