Dinesh
Dinesh

Reputation: 11

finding class Name

I like to know "how to find class name from a .class file". I hope you will explain this as clearly as possible,because I know only basics about java.

Upvotes: 1

Views: 189

Answers (3)

emory
emory

Reputation: 10891

A java class file is a data structure that follows the specified format.

ClassFile {
    u4 magic;
    u2 minor_version;
    u2 major_version;
    u2 constant_pool_count;
    cp_info constant_pool[constant_pool_count-1];
    u2 access_flags;
    u2 this_class;
    u2 super_class;
    u2 interfaces_count;
    u2 interfaces[interfaces_count];
    u2 fields_count;
    field_info fields[fields_count];
    u2 methods_count;
    method_info methods[methods_count];
    u2 attributes_count;
    attribute_info attributes[attributes_count];
}

Constant pool data entries follow the following general specified format where tag determines the length of info.

cp_info {
u1 tag;
u1 info[];

}

except if tag=CONSTANT_Utf8 then the following specified format is followed.

    CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}
  1. Read constant_pool_count - It is at a fixed position in the structure. constant_pool_count = the size of the constant pool + 1. Because the constant pool is not a fixed size structure, it is necessary to know the constant_pool_count to read the rest of the structure.
  2. Read through the constant pool. The beginning of the constant pool is at a fixed position. Read the tag of the ith entry. If tag==CONSTANT_Utf8_info, you need to read the length, otherwise you can determine the length of the entry by the tag alone. Regardless you know the starting location of the i+1th entry. Repeat this procedure until you have finished the constant pool. Record the starting location of all constant pool entries.
  3. Now you know the location of this_class. It is fixed relative to the end of the constant pool. It is an index into the constant pool.
  4. Since you recorded the starting location of all constant pool entries, you can look it up. You should find that the tag==Constant_ClassInfo and its info is also an index into the constant pool.
  5. Look it up. You should find that the tag==Constant_Utf8_info. The info associated with this entry is the fully qualified class name encoded in internal form. "java/lang/Thread" instead of "java.lang.Thread".

.This will probably be related to the name of the file.

Upvotes: 3

Lyle
Lyle

Reputation: 3793

The class name is usually, but not always, the name of the file. Try using the following command:

javap -public -classpath . FileName

Where "filename" does not include the ".class" suffix.

javap is the Java class file disassembler, and the -public switch will show you the public classes and members. In Java, the classpath is the directory or directories which the Java runtime will look in for class files. Substitute the "." for the directory of the file you're interested in if it's not in your current directory.

Upvotes: 1

plor
plor

Reputation: 282

Usually the class name is what precedes the .class, so ClassName.class is the convention.

Upvotes: 0

Related Questions