The easiest way is probably using something like ASM:
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.commons.EmptyVisitor;
public class PrintClassName {
public static void main(String[] args) throws IOException {
class ClassNamePrinter extends EmptyVisitor {
@Override
public void visit(int version, int access, String name, String signature,
String superName, String[] interfaces) {
System.out.println("Class name: " + name);
}
}
InputStream binary = new FileInputStream(args[0]);
try {
ClassReader reader = new ClassReader(binary);
reader.accept(new ClassNamePrinter(), 0);
} finally {
binary.close();
}
}
}
If you can't use a 3rd party library, you could read the class file format yourself.