问题
Is there some tool or technique to let me query the contents of a Java .class file? I want to know the fully qualified name of the class and what method it defines. I thought that perhaps javap
could help me with this but I can't figure out how.
回答1:
Is the class contained in the .class file always the same as the file name (excluding .class)
In practice, yes. Every Java implementation I've come across relies on class file pathnames to locate classes to be loaded, etc.
In theory, a classloader could use some other mechanism, and therefore could store classes in files that bear no relation to the class names. However, I see no advantage in doing that.
even if I use some other language such as Groovy, Jython or Scala?
Given that these languages compile to bytecode files that are then loaded by standard class loaders, then it is pretty much a given that the class names in the bytecode files correspond to the file pathnames ... otherwise the class loaders wouldn't be able to locate the class files. However, this does not necessarily mean that the class names in the bytecode files are identical to Scala / Jython / Groovy the identifiers used in the source code. Indeed, this isn't even true for Java; e.g. the classfile names for anonymous and inner classes for not the same as the identifiers in the Java source code.
回答2:
Call javap ClassName
(not javap ClassName.class
).
If your .class
just to javap ClassName
If your .class
is in a subfolder javap path/to/ClassName
or javap path.to.ClassName
Resources :
- oracle.com - man javap
回答3:
Try this Java Decompiler. Should give you the information you want.
来源:https://stackoverflow.com/questions/3923799/tool-to-check-package-name-and-class-name-of-the-class-within-a-class-file