obtaining line of codes from a class file

i'm currently developing a metric tool (e.g., depth of inheritance, number of private methods, number of public fields and etc). my input(s) to the tool is(are) java class(es)- (.class extension).

i wonder is it possible to obtain metrics such as LINE OF CODES (line of comments, line of executable codes and etc) just from java classes (files with .class extension).

some samples are appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I would venture to say that .class files don't have lines of code being that they're binary files.  And also, .class files don't have comments.  The compiler strips them out.

However, you might be able to get fields, methods, etc. using the java.lang.reflection package.

For methods available on a class see:


So to answer your question: my guess is no.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ervinlohAuthor Commented:
to mraible,

getting fields, methods and etc is not a problem. only line of codes is giving me the problems.
 You can get the lines of code from the Java source file (if it is available) and not from the class file itself. Just read the file and check for the "\n" character (newline).

  Hope it helps.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Can't get source code from class file. Only from source file.
Mick BarryJava DeveloperCommented:
You can use a decompiler to generate source from a class file. However this will not be the same as the source code before it was compiled so i'm not sure of its value for metrics purposes. But it may help you.

The class file does not have lines. The compiler strips comments and blank line. It also generates binary code instead of the source code, which means the source code is not available any more. The compilation process lossses the information about number of lines, comments, and the explicit code written.
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to mraible

Please leave any comments here within the
next seven days.


Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.