1

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?

JohnRaja
  • 2,377
  • 6
  • 26
  • 47

5 Answers5

6

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).

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I want to create object dynamically, the class name will be differ for every time. Any solution plz? – JohnRaja Feb 24 '11 at 13:48
  • @user: Only the two solutions above: Don't obfuscate the class you're trying to create, or use a feature of your obfuscator (if it has one) that rewrites the string version of the name in the bytecode for you. – T.J. Crowder Feb 24 '11 at 13:57
0

When you obfuscate the code it changes class name by some a,b' and so on..

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    *"check for exact class name in Obfuscated state"* Well, yeah, but you can't rely on it being the same from obfuscation to obfuscation, can you? I mean, not if you change anything in the meantime. – T.J. Crowder Feb 24 '11 at 13:16
  • @T.J. aah..yes, we shouldn't not relay on that. – jmj Feb 24 '11 at 13:25
0

Obfuscation Changes the Tokens, Identifiers so your hard coded string ("org.test.Menu") for name wouldn't be found.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
0

Obfuscation changes the names of classes and so the name of class Menu will be changed to something else.

Nick
  • 25,026
  • 7
  • 51
  • 83