• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the size of .class file using java program with out using .IO package?

 
michel dughlas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently i attended a interview.there i faced this question any one Please help me in this. the question is How to find the size of .class file using java program with out using .IO package?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you write your own ClassLoader, and use that to load the class, you'll get at the byte[] that makes up the class in the defineClass method. Then you know how many bytes the class file contains.
 
michel dughlas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply Ulf Dittmer. if you dont mind can you provide me a example on this.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this article on how to write a ClassLoader.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not really get the motive behind asking such questions in an interview. I will assume that the interviewer just wants to say that "Hey you know what i have written ClassLoaders and there is this method defineClass() that takes byte[] array that has the entire class definition." Boy, put a blog entry telling the world that you know that! But why the hell you want to ask that in an interview and how will it help me even if i know that part of the api.
Sorry if i have hijacked the topic but i really get irritated if i see people doing such things. Interviews are not to tell the candidate that the interviewer knows alot, it is for knowing how much does the candidate know that will be beneficial for the company.
Anyways! I think these guys are a part of the industry, so you have to live with it.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitesh, you're discounting the possibility that class loaders might be crucial to that company's work. While using them for this purpose probably isn't what they do, it's a way to gauge the level of understanding the candidate has about class loading.

In interviews many questions are asked whose primary purpose is not to ascertain whether the candidate knows the answer, but to get him to talk about a problem and its possible solutions. This would seem to be an example of that.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually...

Ulf, I may be lacking in imagination, but I can't see how to accomplish this the way you say. The defineClass() methods are final, so you can't trick a superclass ClassLoader into handing you the bytes by overriding them.

My solution would use a ClassLoader too: use a ClassLoader.getResourceAsStream() call to get the .class file (ask for it by name, it works!) and then count the bytes you can read from the stream. You'd probably use a URLClassLoader so you could tell it where to look for this specific class file.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that stream you get is a java.io.InputStream, and the IO package is disallowed for this question. As an alternative, there's this:

That's not as reliable as using IO to actually read the file, but I think it may be what they were looking for.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a silly question - no matter what tricks you pull in Java, the IO package will eventually be involved - thats just the way the language is designed.

Bill
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Nitesh, you're discounting the possibility that class loaders might be crucial to that company's work. While using them for this purpose probably isn't what they do, it's a way to gauge the level of understanding the candidate has about class loading.

I agree with you Ulf but i personally feel that there are much better way of finding out how much a candidate knows about classloading.

Originally posted by Ulf Dittmer:

In interviews many questions are asked whose primary purpose is not to ascertain whether the candidate knows the answer, but to get him to talk about a problem and its possible solutions.

Agree, such interviews are much better than having an interview where there are only trick questions and *only* one answer that the interviewer knows
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic