No db connectivity when I run *.jar
While in Eclipse, everything works fine. I have an initial Login Screen GUI which takes you to another frame when login is successful.
I have included mysql library in the build path. Still when I create a jar file and run it, the initial login Screen will appear, but I'm unable to get past it. Login always fails.
I have MySQL database installed on my system //localhost
What could possibly be the reason?
[435 byte] By [
aurir_a] at [2007-11-26 18:57:37]

# 1
If it helps this is the code that is executed when the login button is clicked:
//Attempt login
SqlBackend.DbConn con = new SqlBackend.DbConn(userName, password);
if(con.getLoginSuccessful()){
MainJFrame newWindows = new MainJFrame(con);
frame.dispose();
}else{
txtUserName.setText("");
pass.setText("");
lblFailed.setText("LOGIN FAILED");
}
getLoginSuccessful() returns the boolean variable loginSuccessful which is initialiy set to TRUE and is set to FALSE only when connection fails:
public DbConn(String user, String pass){
try{
//Loading a driver. No need to create instance
Class.forName("com.mysql.jdbc.Driver");
//Establish connection
//String url = "jdbc:mysql://localhost/coffeebreak";
conn = DriverManager.getConnection(url, user, pass);
//Handles both SQLException and ClassNotFoundException for the driver
}catch(Exception e){
loginSuccessful = false;
System.out.println(e.getMessage());
}
}
# 2
> While in Eclipse, everything works fine. I have an
> initial Login Screen GUI which takes you to another
> frame when login is successful.
> I have included mysql library in the build path.
When you create the executable JAR, you either have to add the Class Path to the manifest, listing all the JAR files that your code uses, OR execute the JAR with the -classpath option.
My guess is that you haven't told the JVM where to find the MySQL JDBC driver JAR. Eclipse knows where to find it because you added it to the Eclipse project build path, but you haven't done the equivalent when you run the executable JAR.
> Still when I create a jar file and run it, the
> initial login Screen will appear, but I'm unable to
> get past it. Login always fails.
What does "fails" look like? Is there an exception thrown? Please post the stack trace.
> I have MySQL database installed on my system //localhost
Not the likely cause.
%
# 3
I have modified my manifest.mf to include the class path to the mysqlconnector (it is all in Windows environment) and when I run the jar I get:
Could not find the main class. Program will exit;
My manifest.mf looks like
Manifest-Version: 1.0
Sealed: true
Main-Class: GUI.LoginScreen
Class-Path: C:/Program Files/MySQL/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar
When I delete Class-Path entry the jar has no problems finding main class
# 4
you didn't do it correctly, then.%
# 5
Are you gonna tell me what is the correct way to do it?
# 6
I'd have to see the whole JAR file to know what's wrong.
I'd recommend that you make the path of each JAR that you need relative to the executable JAR. An absolute path doesn't seem like a very good idea. Paths with spaces in them will be problematic, too.
Make sure that the .class file with the main() method you want to run appears in the JAR, and that its path is correct.
%
# 7
Ok, I've tried a lot of stuff. I have modified my application to display error message instead of "Login Failed".
It says: "com.mysql.jdbc.Driver" so like you said the mysql jar is missing.
I have copied the whole mysql-connector-java-5.0.4-bin directory into my application directory. My manifest.mf looks like:
Manifest-Version: 1.0
Class-Path: mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.
jar
Sealed: true
Main-Class: GUI.LoginScreen
I have also tried changing names to mysql/mysql.jar.
I have also tried coping just the jar file in the project folder
Class-Path: mysql-connector-java-5.0.4-bin.jar
There is also some .classpath file in the jar and it looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="F:/CurrentDocuments/Fall2006/JavaProjectsTemp/RentItOut/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
I don't know. According to the:
http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form:
Class-Path: jar1-name jar2-name directory-name/jar3-name
By using the Class-Path header in the manifest, you can avoid having to specify a long -classpath flag when invoking Java to run the your application.
END OF QUOTATION.
Tomorrow I'll try doing it manually under Linux. Maybe Eclipse is doing something. I don't know
# 8
Ok, I have figured out what my problem was. Basically speaking, I wasted your and my time.
Even though I copied mysql-connector folder into my project folder and included
class-path: mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar in the manifest.mf, the jar file that I was creating was outside of the project folder. So obviously the path specified was not working. So stupid of me.
Anyways, I've learned that the absolute path is not working so maybe it was not a total waste of time.
Thanks!