Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes class names & loading: Java 5 vs Java 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "class names & loading: Java 5 vs Java 6" Watch "class names & loading: Java 5 vs Java 6" New topic
Author

class names & loading: Java 5 vs Java 6

M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
It seems like variants of this little program are popular on the web:



This works under Java 5 but throws an exception under Java 6. From what I can tell, the loadClass() method in Java 6 strictly enforces a "binary string" which not what is returned by the getName() method for an array.

Can anyone comment on this? It seems to have an impact on several open-source projects. I can't tell if this is an anticipated "tightening" of Java or a bug in the current Java 6 release. I've searched the web; there are references to the problem but no one is talking about the change per se.

thanks,
Mike


M Easter
Software Composer - http://codetojoy.blogspot.com
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
Looking at the 1.5 javadocs for Class.getName, it states that getName returns the binary string (which is required for ClassLoader.loadClass) only for objects that are not arrays or primitives. For primitives the issue of class loading is moot, but it would not have occurred to me that the result of getName for an array can be used to load the component type of the array.

It should be easy to extract a usable name from the representation returned by getName, though.


Android appsImageJ pluginsJava web charts
M Easter
Ranch Hand

Joined: Feb 11, 2007
Posts: 133
I guess my question is: why the change now in Java 6?

i.e. is this considered a correction or a bug

See http://tinyurl.com/2x6gc8 (implies it is a bug)

thanks,
Mike
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by M Easter:
I guess my question is: why the change now in Java 6?

i.e. is this considered a correction or a bug

See http://tinyurl.com/2x6gc8 (implies it is a bug)

thanks,
Mike

It doesn't just imply, it states it is a bug.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
That report does state it's a bug, but see also commentary here. Sounds like the preferred solution here is to simply use Class.forName() instead.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class names & loading: Java 5 vs Java 6
 
Similar Threads
Exception questions
NoInitialContextException
what is the replacement for BatchEnvironment ?
what is the replacement for BatchEnvironment ?
NoInitialContextException