This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to find the size of .class file using java program with out using .IO package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find the size of .class file using java program with out using .IO package?" Watch "How to find the size of .class file using java program with out using .IO package?" New topic
Author

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

michel dughlas
Greenhorn

Joined: Oct 31, 2007
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
michel dughlas
Greenhorn

Joined: Oct 31, 2007
Posts: 6
Thank you for your reply Ulf Dittmer. if you dont mind can you provide me a example on this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Have a look at this article on how to write a ClassLoader.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12676
    
    5
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

Joined: Feb 25, 2007
Posts: 1638

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the size of .class file using java program with out using .IO package?
 
Similar Threads
DOUT ON PACKAGES.
File Class
Help needed (.tar.gz file)
Accessing local directory from a packaged class
How can we get file Inode info using java?