开发者

CLASSPATH issue while accessing Mysql on Linux

I have Mysql installed on my Linux box and wrote a sample program to access one of it's table. I am using 'mysql-connector-java-5.1.10.jar'

The code is working fine if i put the jar in 'jre/lib/ext'. However, other ways of recognizing that jar are not working. I tried with setting $CLASSPATH and tried to use '.' current directory.

It's failing with the following error :

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306
        at java.sql.DriverManager.getConnection(DriverManager.java开发者_如何学Python:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)


I usually don't use the global $CLASSPATH variable, the easiest way to get it running is

java -cp .;/path/to/mysql-connector-java-5.1.10.jar[;<other libs>] pkg.name.MyApplication

Sidenote

If you have your application exportet to a jar with a Main-Class attribute ("executable jar") and start it with java -jar myjar.jar, then you have to add all required libraries to the jars manifest, $CLASSPATH and -cp are ignored in this case. And that's why I usually don't use the -jar option...

Edit

To answer your additional question: If the current directory was added to the classpath by default, then the location from where the application was started could influence the application itself.

Imagine an application inside a jar and a start command

 java -cp application.jar com.example.Main

Now we have a defined environment: only the content of application.jar (and the jre classes) are on the classpath and part of the application. If the current directory was added to the classpath automatically then all files at the current location (and at locations of all subfolders) would be on the classpath too, intend or not. With a result, that the application might work if started from the users home directory but maybe not if started from the root directory (/).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜