Question

I have a *.jar file and i want to use some classes from it. Among the others, it contains class "a" from package "com.a.a" and package "com.a.a.a.a.e" with class "a". When i try to access the class as "com.a.a.a.a.e.a", there is an error: com.a.a.a.a cannot be resolved to a type. It happens because compiler tries to get the field "a" of class "com.a.a.a" rather than package "com.a.a.a.a". I also tried to import this class, but it makes no difference, and i get the error "The import com.a.a.a.a cannot be resolved".

Is there any way to use the class "com.a.a.a.a.e.a"? Are there any tools that are able to rename classes in *.jar? Or may be reflection can help here?

Was it helpful?

Solution

The JAR you're trying to use has most likely been obfuscated, probably for the purpose of preventing what you're trying to do. Some Java obfuscators change the package, class, method, and variable names to violate the JLS but still be valid compiled bytecode; that appears to be exactly what has been done to this 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; everything else is scrambled to prevent you from de-compiling it and/or using it directly.

OTHER TIPS

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. 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. I suspect that your jar has been put through an obfuscation process, which, depending on how it was done, can make renaming classes complicated.

Use Java Code Conventions - http://www.oracle.com/technetwork/java/codeconv-138413.html - and you will NEVER have such errors.

If you want to access jar - decompile it, fix it and assemble it back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top