简体   繁体   English

使用与* .jar中的类相同的名称访问包

[英]Accessing the package with the same name as a class in a *.jar

I have a *.jar file and i want to use some classes from it. 我有一个* .jar文件,我想从中使用一些类。 Among the others, it contains class "a" from package "com.aa" and package "com.aaaae" with class "a". 其中,它包含来自程序包“ com.aa”的类“ a”和具有类“ a”的程序包“ com.aaaae”。 When i try to access the class as "com.aaaaea", there is an error: com.aaaa cannot be resolved to a type. 当我尝试以“ com.aaaaea”的形式访问该类时,出现错误:com.aaaa无法解析为一种类型。 It happens because compiler tries to get the field "a" of class "com.aaa" rather than package "com.aaaa". 发生这种情况是因为编译器尝试获取类“ com.aaa”的字段“ a”而不是包“ com.aaaa”。 I also tried to import this class, but it makes no difference, and i get the error "The import com.aaaa cannot be resolved". 我也尝试导入此类,但是没有区别,并且出现错误“导入com.aaaa无法解析”。

Is there any way to use the class "com.aaaaea"? 有什么方法可以使用“ com.aaaaea”类吗? Are there any tools that are able to rename classes in *.jar? 是否有任何工具可以重命名* .jar中的类? Or may be reflection can help here? 还是反射可以在这里有所帮助?

The JAR you're trying to use has most likely been obfuscated , probably for the purpose of preventing what you're trying to do. 您尝试使用的JAR很可能已被混淆 ,可能是为了防止您尝试执行的操作。 Some Java obfuscators change the package, class, method, and variable names to violate the JLS but still be valid compiled bytecode; 某些Java混淆器会更改程序包,类,方法和变量名,以违反JLS,但仍是有效的编译字节码。 that appears to be exactly what has been done to this JAR. 这似乎正是对此JAR所做的事情。 It will be very difficult to make sense of the de-compiled code in that case. 在这种情况下,很难理解反编译的代码。

JARs that are obfuscated usually leave just their public API un-obfuscated for your code to use; 混淆的JAR通常只对公共API进行混淆,以供您使用代码。 everything else is scrambled to prevent you from de-compiling it and/or using it directly. 扰乱了其他所有内容,以防止您反编译和/或直接使用它。

Quote from the JLS: 捷豹路虎的报价:

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. 在可能将其解释为变量,类型或包的名称的上下文中,可能会出现一个简单的名称。 In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. 在这些情况下,第6.5节的规则指定将优先于类型选择变量,并且优先于包选择类型。 Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. 因此,有时可能无法通过其简单名称引用可见类型或程序包声明。 We say that such a declaration is obscured. 我们说这样的声明是模糊的。

Reflection could help working around the problem. 思考可以帮助解决问题。 Although renaming classes can be a solution too in most situations, well-behaved jars shouldn't even have this problem. 尽管在大多数情况下重命名类也是一种解决方案,但是行为良好的jar甚至不应该出现此问题。 I suspect that your jar has been put through an obfuscation process, which, depending on how it was done, can make renaming classes complicated. 我怀疑您的jar已经过混淆处理,根据混淆的过程,它会使重命名类变得复杂。

Use Java Code Conventions - http://www.oracle.com/technetwork/java/codeconv-138413.html - and you will NEVER have such errors. 使用Java代码约定-http: //www.oracle.com/technetwork/java/codeconv-138413.html-您将永远不会遇到此类错误。

If you want to access jar - decompile it, fix it and assemble it back. 如果要访问jar-对其进行反编译,请修复并重新组装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM