This discussion is archived
13 Replies Latest reply: Feb 8, 2010 11:40 AM by 843798 RSS

a few question about Jar file importing in runtime

843798 Newbie
Currently Being Moderated
Hey guys,

I was wondering, I have a Jar I would like to import into my application, now... I was wondering, how can I get a data file, not a class object, that is in a jar file? I would like to read a few things from a data file before loading any jar or class...

Thanks in advance,
Adam.
  • 1. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Nothing to do with reflection,
    you probably want [ClassLoader.getResourceAsStream()|http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream%28java.lang.String%29].
  • 2. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    well you are right, as I started this thread I was thinking about another question regarding importing a new Jar dynamically into the application, but as I was thinking about it, I do not know the names of the classes that I want to create, so I thought about putting them in a data file first, with a few more information, this way I could classify the import of the Jar to their right place, and still give me the freedom to name the objects what ever I like, and store all the plugins in the same folder...

    So please, if I have an external Jar on c:\externalJar.Jar and in it I have a file c:\externalJar.Jar\data.dat, how do I retrieve the stream of the file data.dat?
  • 3. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    thanks but I got it
  • 4. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    well, here is the question I was really going to ask:

    I have, X amount of jar to import, Y of them are plugins of type1, and Z of them are plugins of type2, and so on. I added a line in the manifest to define types of the plugins, and the plugin main class, but here is the thing I like to be consistent so I would like to have a constant folder structure in all of the plugins, for example, I would like to have:

    pluginX.jar/plugin/*.*
    pluginY.jar/plugin/*.*
    pluginZ.jar/plugin/*.*

    now here is what I wonder, if I put the same data file in all of the jars, for example data.dat, and since I added all the Jars to the class loader, I would get the first occurrence in the first jar file loaded when using getResource() method. am I right about this?

    furthermore I would like to generate manifests for my jars, and not write them down, one by one. I don't have many plugins to load and doing it manually is not such a big deal, this more of a personal preference to have everything done automatically. is there a technic or a generator, or am I doomed to do this manually?

    thanks in advance,
    Adam.
  • 5. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Adam-Z. wrote:
    I have, X amount of jar to import
    import?
    You import classes at compile time, not run time.
    I guess you mean make the classes in a jar accessible to be loaded.
    Y of them are plugins of type1, and Z of them are plugins of type2, and so on. I added a line in the manifest to define types of the plugins, and the plugin main class, but here is the thing I like to be consistent so I would like to have a constant folder structure in all of the plugins, for example, I would like to have:

    pluginX.jar/plugin/*.*
    pluginY.jar/plugin/*.*
    pluginZ.jar/plugin/*.*

    now here is what I wonder, if I put the same data file in all of the jars, for example data.dat, and since I added all the Jars to the class loader, I would get the first occurrence in the first jar file loaded when using getResource() method. am I right about this?
    If you create a URLClassLoader for each jar
    you can pick the data.dat file you want.
    furthermore I would like to generate manifests for my jars, and not write them down, one by one. I don't have many plugins to load and doing it manually is not such a big deal, this more of a personal preference to have everything done automatically. is there a technic or a generator, or am I doomed to do this manually?
    Maybe Apache ant?
  • 6. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Thank you for a super quick reply,
    You import classes at compile time, not run time.
    I guess you mean make the classes in a jar accessible to be loaded.
    well semantics, I really don't have all the term figured out yet, I was never good with terms, I would like to think I'm pretty good with implementation though... yes I mean to make the classes accessible for loading, to add a jar file to the build path dynamically.
    If you create a URLClassLoader for each jar
    you can pick the data.dat file you want.
    oh. I see now, yes since it reference to the the specific jar great thanks, although I implemented a single URLClassLoader,
    which brings me to my next question, once I've loaded a Class file from the class loader, it is saved in the Vector classes of the ClassLoader object. Is there any way in the world to implement a remove method? I tried around a bit but there is no way i could see to get the vector field...
    Maybe Apache ant?
    don't know never used it... yet.

    Thanks in advance,
    Adam.
  • 7. Re: a few question about Jar file importing in runtime
    EJP Guru
    Currently Being Moderated
    well semantics
    Yes, semantics, the meaning of the words. If you don't use the terminology correctly you make it harder for people to understand you. Self-interest really.
    I was never good with terms
    I suggest you start fixing that now. It's a terminology-ridden profession (always assuming it's a profession ;-))
    once I've loaded a Class file from the class loader, it is saved in the Vector classes of the ClassLoader object. Is there any way in the world to implement a remove method?
    No. All you can do is let the URLClassLoader and all the classes it loaded and all the objects they created be garbage collected.
  • 8. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Thanks for the tips,
    I suggest you start fixing that now. It's a terminology-ridden profession (always assuming it's a profession ;-))
    would do as soon as I find the time... honest.
    No. All you can do is let the URLClassLoader and all the classes it loaded and all the objects they created be garbage collected.
    oh... I see now, yes that is interesting approach, to gc the entire class loader... nice, but doesn't the classloader parent has a reference to this classloader I want to garbage, if I remove its only reference in my code how do I make sure that there is no reference left at the parent?

    Thanks,
    Adam.
  • 9. Re: a few question about Jar file importing in runtime
    DrClap Expert
    Currently Being Moderated
    Adam-Z. wrote:
    but doesn't the classloader parent has a reference to this classloader I want to garbage
    No. A classloader certainly has a reference to its parent, but I don't believe a classloader also has links to all classloaders which nominated it as their parent. If it did, the advice to let the classloader be garbage-collected wouldn't work. And it does work.
  • 10. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Adam-Z. wrote:
    ... gc the entire class loader... nice, but doesn't the classloader parent has a reference to this classloader I want to garbage,
    if I remove its only reference in my code how do I make sure that there is no reference left at the parent?
    The system class loader loaded the URLClassLoader class, so it has a reference to the URLClassLoader class.
    It does not have a reference to the URLClassLoader instance you used to load some classes.
  • 11. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    thanks guys, you were all really helpful, Thanks.

    BTW
    I need to know, if I have some code I would like people to look at and give me an honest opinion, about the implementation where can I post it? I mean is it acceptable to post it here for that purpose ...

    Thanks,
    Adam.
  • 12. Re: a few question about Jar file importing in runtime
    DrClap Expert
    Currently Being Moderated
    If it's less than about 100 lines of code you could post it here. If it's a lot more than that, then distill it down and post the important part. And don't forget to wear your flame-proof underpants.
  • 13. Re: a few question about Jar file importing in runtime
    843798 Newbie
    Currently Being Moderated
    Thanks