Bug #83052 static method in com.mysql.jdbc.Util relies on null object
Submitted: 20 Sep 2016 9:52 Modified: 8 Feb 2017 23:38
Reporter: kai ma Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:5.1.39 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any

[20 Sep 2016 9:52] kai ma
Description:
Recently,I caught the NoClassDefFoundError of com.mysql.jdbc.Util  when I start my project adding the Spring load time weaver.Trace the error I found that there is an NullpointerException in the static method of com.mysql.jdbc.Util. Here is the code:

        packageName = MultiHostConnectionProxy.class.getPackage().getName();

Using Spring ltw the getPackage method  of Class Object will return null.
I did create a bug for spring :https://jira.spring.io/browse/SPR-14736.
But as Juergen Hoeller  said on the reply, com.mysql.jdbc.Util  should not relies on non-null method result.

Hoping for the fix as soon as possible.THX!

How to repeat:
my email is sherbanimin@gmail.com and  360245538@qq.com
Hoping for reply soon!
[20 Sep 2016 11:57] Chiranjeevi Battula
Hello kai ma,

Thank you for the bug report.
I tried to reproduce the issue at my end using MySQL Connector / J 5.1.39 but not seeing any issues in getPackage().getName().
Could you please provide us repeatable test case (steps, sample code, full stack trace etc. - please make it as private if you prefer) to confirm this issue at our end?

Thanks,
Chiranjeevi.
[23 Sep 2016 6:44] kai ma
Hi Chiranjeevi Battula,

just use 
"packageName = MultiHostConnectionProxy.class.getName().replace(".MultiHostConnectionProxy","");"

will fix the issue.

My team is looking forward for the fix version.

Thank you!
[23 Sep 2016 12:06] Chiranjeevi Battula
Hello kai ma,

Thank you for the bug feedback.
Is there any change you could get us a reproducible test case? I understand that you already tried to upload it but it was too large, so, maybe you could just describe all the steps you did to reproduce it? A "hello world" example would be great. I see that you are using spring, so please also include the all the minimal procedures to get spring required configurations. Please assume that we don't have a spring env to test and we need to prepare all from scratch.

I really need this because we rely on the method getPackage() in more that one place and I want to know that we won't have any other problems.

Btw, I understand what you suggested as a fix, but to me it seems more a bad behavior in the spring component you are using... Anyway, we'll see what we can do about it.

Thanks,
Chiranjeevi.
[29 Sep 2016 11:57] Chiranjeevi Battula
Hello kai ma,

Thank you for the bug feedback.
We'll try to do our best with the information you provided but a working test case or the steps to get it would be much appreciated.

Thanks,
Chiranjeevi.
[30 Oct 2016 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[2 Nov 2016 1:25] Bill Reck
Hi,
I've had problems with this as well. As I understand it Class.getPackage() can return null under with some classloaders.  For example:

//import org.springframework.core.OverridingClassLoader;
File archive = new File("[somepath]/mysql-connector-java-5.1.39.jar");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { archive.toURI().toURL() });
OverridingClassLoader over = new OverridingClassLoader(urlClassLoader);
Class clzz = over.loadClass("com.mysql.jdbc.MultiHostConnectionProxy");
System.out.println("loaded class: " + clzz.getCanonicalName());
Package pack = clzz.getPackage();
// pack is null here
System.out.println("package: " + pack);
String name = pack.getName();
System.out.println("package: " + name);
[4 Nov 2016 12:14] Chiranjeevi Battula
Hello Bill Reck,

Thank you for the bug feedback and testcase.
Verified this behavior on MySQL Connector / J 5.1.40.

Thanks,
Chiranjeevi.
[4 Nov 2016 12:14] Chiranjeevi Battula
output:
run:
loaded class: com.mysql.jdbc.MultiHostConnectionProxy
Exception in thread "main" java.lang.NullPointerException
package: null
	at javaapplication3.bug_83052.test(bug_83052.java:45)
	at javaapplication3.bug_83052.main(bug_83052.java:31)
C:\Users\cbattula\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
[8 Feb 2017 23:38] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 5.1.41 changelog:

"When loading classes through external class loaders, com.mysql.jdbc.Util threw an NoClassDefFoundError. This was caused by Class.getPackage() always returning null when external class loaders were used. This fix replaces those calls of Class.getPackage() with calls of the new method Class.getName(), which return package names that are extracted from the full-qualified class names."