在 j2me 中进行混淆时,Class.forName 中出现 ClassNotFoundException
我能够运行以下示例代码,
//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");
}
}
它运行良好,但是当我混淆代码时,我得到一个 no ClassNotFoundException。
我正在使用 netbean 6.9.1 。在附加混淆设置中,我添加了 -keepnames 类 org.test.Menu。但仍然不起作用。有什么解决办法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
原因很简单:混淆器将 org.test.Menu 的名称更改为其他名称(包名称更改和/或类名称更改)。并且混淆器无法“重构”类,以便其他类文件中基于字符串的类名也被更改。
如果是这种情况,请告诉混淆器不要触及 org.test 包(保留该名称并且不要混淆内部类的名称)。
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).这是设计使然。混淆会更改所有公共标识符的名称(包括类名称),因此如果您通过字符串引用其中任何一个(例如使用
Class.forName
或其他形式的反射),尤其是您计算出的会损坏的字符串 ("org.test." + a
)。如果您需要通过
Class.forName
按需加载Menu
,则无法混淆Menu
类。自从我研究混淆器以来已经很长了,但是如果您以特定方式标记它们,IIRC 可能能够为您重写一些字符串;检查您正在使用的文档,看看是否可以。但即便如此,他们也不太可能重写诸如“org.test”之类的内容。 + 给你一个。您必须在单个字符串中包含全名。
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
viaClass.forName
, then you cannot obfuscate theMenu
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.混淆 更改令牌、标识符,以便您的硬编码字符串 (
"org.test.Menu "
) 找不到名称。Obfuscation Changes the Tokens, Identifiers so your hard coded string (
"org.test.Menu"
) for name wouldn't be found.混淆会更改类的名称,因此类 Menu 的名称将更改为其他名称。
Obfuscation changes the names of classes and so the name of class Menu will be changed to something else.
当您混淆代码时,它会通过一些
a
、b
'等更改类名称。When you obfuscate the code it changes class name by some
a
,b
' and so on..