Reputation: 2357
I am able to run the following example code
//main class
String a="Menu";
Object o = Class.forName("org.test."+a).newInstance();
//Menu class
public class Menu()
{
public Menu()
{
System.out.println("con called");
}
}
It runs fine, but when I obfuscate the code I get a no ClassNotFoundException.
I am using netbean 6.9.1 . In Additional obfusating setting i added -keepnames class org.test.Menu. But still not working. Any solution?
Upvotes: 1
Views: 954
Reputation: 240870
When you obfuscate the code it changes class name by some a
,b
' and so on..
Upvotes: 0
Reputation: 1074168
This is by design. Obfuscation changes the names of all of your public identifiers (including class names), and so if you're referring to any of them via strings (like with Class.forName
, or other forms of reflection), and especially strings that you calculate ("org.test." + a
) that will break.
If you need to demand-load Menu
via Class.forName
, then you cannot obfuscate the Menu
class.
It's been a long time since I looked at obfuscators, but IIRC some may be able to rewrite some strings for you if you tag them in a particular way; check the docs on the one you're using to see if it can. But even then, it's unlikely they'd be able to rewrite something like "org.test." + a
for you. You'd have to have the full name in a single string.
Upvotes: 1
Reputation: 25799
Obfuscation changes the names of classes and so the name of class Menu will be changed to something else.
Upvotes: 0
Reputation: 18420
Obfuscation Changes the Tokens, Identifiers so your hard coded string ("org.test.Menu"
) for name wouldn't be found.
Upvotes: 0
Reputation: 114767
The trivial reason: The obfuscator changed the name of org.test.Menu
to something else (package name changed and/or class name changed). And the obfuscator can't "refactor" the classes so that String based class names in other class files are changed too.
If this is the case, tell the obfuscator not to touch the org.test
package (keep that name and don't obfuscate the name of the class(es) inside).
Upvotes: 6