• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on loading class from Jar File

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I search through a jar file to find a ".class" file, how do I know the proper package it's in? For example, if the jar file has it in the directoy:

src/com/somecompany/SomeClass

It will try to create the class "src.com.somecompany.SomeClass" but src is not a package. I need to create it starting at "com." How do people handle this?
[ May 12, 2008: Message edited by: Dan Bizman ]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this jar -cvf my.jar com*( instead of com* , you can place whatever package you want to include).

Hope it helps.

_____________________
Mintoo
SCJP 1.4
______________________
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mintoo kumar:
try this jar -cvf my.jar com*( instead of com* , you can place whatever package you want to include).

Hope it helps.

_____________________
Mintoo
SCJP 1.4
______________________



Unfortunately that's not what I'm looking for. I'm going to have to examine the byte code to determine the package (in the constant pool). Shouldn't be too hard.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dan:
I'm going to have to examine the byte code to determine the package (in the constant pool).


(Although I dont know what do you intend when you say "in the constant pool" but I will try to answer.)

You mean that you will scan all the entries in the jar file and assert the package of the class by removing src from the path?
OR
You want to parse every entry in the jar and then determine the package by reading the compiled byte code?

If you can not change the jar file, I think you will be better off exploding the jar file onto the file system and add src/ to the classpath rather than doing all the jugglery.

I strongly feel that the structure of the jar must be changed to include the path from com and not from src.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nitesh Kant:

(Although I dont know what do you intend when you say "in the constant pool" but I will try to answer.)

You mean that you will scan all the entries in the jar file and assert the package of the class by removing src from the path?
OR
You want to parse every entry in the jar and then determine the package by reading the compiled byte code?

If you can not change the jar file, I think you will be better off exploding the jar file onto the file system and add src/ to the classpath rather than doing all the jugglery.

I strongly feel that the structure of the jar must be changed to include the path from com and not from src.



No, my project cannot know before the structure of the jar files it will be handed, it only knows there are class files (and other files inside). So it classes may be inside "src" directory or "java" or "blah-blah" we won't know. So I'll examine the bytecode (meaning, we look for 0xCAFEBABE to determine if it's a java class file and then read the beginning data in the file to get the full classname from the constant pool). It's the only way to be certain you're defining it properly.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Nitesh (although I can't find any reference to back this up). The directory structure for a class file in a jar should consist of just the package structure and nothing else. There should be no 'src', 'java' or 'blah-blah' directories.
Do you have an example of a commercial jar file that does have the package structure in a sub directory ?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only reliable and portable way, AFAIK, is to create your own ClassLoader class so you can use the protected method "defineClass(...)" to create a Class object from a byte array. You then just need to read each entry in turn and create run it through this ClassLoader. Once you have a Class, you can use the usual methods to get its name, package, interfaces and superclass, etc.

It should go without saying that life will be easier if you do some basic filtering. E.g., only load .class files, and ideally also check for the java class signature. You'll know there's a serious problem if you still get a ClassFormatError.

There are also byte code libraries, but they obviously require more than the standard java libraries.

Using filenames alone is actually counterproductive. Besides the obvious risk of a misplaced file, what about inner classes? Enumerations?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are convinced that reading the class file to get the complete class name from the Constant pool is the best way of handling this problem, then the
bcel library will help you in doing so.
I still believe that reading every entry of the jar file to get to the constants pool and the class name will be a mammoth task and you will take huge amount of time to load one class.

Disclaimer: I have not used the library but it does promises to do what you want. Reading .class file can be a tedious job if you start doing it yourself.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic