Results 1 to 9 of 9
  1. #1
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Question class files and classpath or ???

    I'm not the original poster but I'm also working on a plugin framework which is kinda working but not as I want it. I can load a interface from a jar file, for example PluginInterface.class that can execute code from the original project. But if I want to have more classes inside the jar file the PluginInterface.class can't recognize them (ClassNotFoundException) and I have to place them in the project to use the classes. So my question is, how can I load the classes to the RAM (?) so I can place the class files in the jar file instead of the project bin. I find it hard to explain how this works, so if you need any further info I can give you some. Or do you want an entirely new thread for this problem?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Java plugin framework advice please....

    Post moved to own thread.
    how can I load the classes to the RAM (?)
    What is the "RAM"?
    place the class files in the jar file instead of the project bin
    Is that a question about how to use an IDE? project is an IDE term.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: class files and classpath or ???

    Yeah, about the RAM thing, I just wondered how I can load classes from a jar file that isn't the original jar that launches the application.

    Plugin Example:
    I have 4 classes

    PluginLoader.class, normal class, inside main.jar - checks for jar files and loads
    PluginInterface.class, an interface, inside main.jar - interface that is needed in a plugin

    Plugin1.class, implements PluginInterface, inside plugin.jar - this is what the PluginLoader is looking for when loading the plugin
    ExtraClass.class, normal class, inside plugin.jar - this is what is needed to execute a certain kind of code in Plugin1


    The Plugin1.class uses the ExtraClass.class to do some sort of execution. But when I run the original main.jar file the application can't recognize the ExtraClass.class (ClassNotFoundException) since it's not in the original jar (I think) or loaded.
    So the thing I want to do is load the classes so I can run them inside plugin.jar. I shouldn't be needing to put the ExtraClass.class inside the main.jar to use the code inside it, right? <- This is what I meant by the project bin

    Hope I clarified some of your questions.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: class files and classpath or ???

    how I can load classes from a jar file that isn't the original jar that launches the application.
    A couple of ways:
    put the other jar file on the classpath using the manifest file's Class-path entry
    use a custom classloader
    put all the jar files on the classpath and specify the starting class's name vs using the manifest via the -jar option
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: class files and classpath or ???

    put the other jar file on the classpath using the manifest file's Class-path entry
    Yeah, I know how I can load a certain kind of jar file that I've specified when I exported the jar file, like a library. The question is more for jar files that another user has made with the class Plugin1.class is inside, I don't think I can add the plugin.jar into the Manifest when the application is already running. I want to be able to add how many other jars I want later. Kinda like DLC or mods for games (minecraft).

    use a custom classloader
    This might be more what I'm looking for

    put all the jar files on the classpath and specify the starting class's name vs using the manifest via the -jar option
    Didn't quite understand this really. Can I have a classpath that loads all the jar file in a certain kind of folder? Is that what you mean?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: class files and classpath or ???

    Can I have a classpath that loads all the jar file
    jar files can be specified using the java command's -cp option:
    java -cp j1.jar;j2.jar;j3.jar TheStartingClass

    I don't know if there is a wildcard that says to use all the jar files in a folder. Read the doc for the java command.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: class files and classpath or ???

    What is a wildcard exactly? Can I give java that "command" when I'm already running the main jar file? Or do I need to specify it before I run the application?

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: class files and classpath or ???

    Hi,
    the common practice is, that it is defined / configured before the application is started. It is just a question of responsibilities:
    - The application developer is not responsible for the locations where everything should be.
    - The system administrator is responsible for the different locations. So he has to know where the different parts reside and make sure that the application finds them (through the classpath).

    But of course it is possible in your code if you think that this separation does not make sense in your case (Which is ok of course. That is no fixed rule. Just a thought to keep stuff where it might belong to.):
    - You can write your own classloader.
    - You could even use a small hack - you can call the addUrl method of the classloader through reflection (It is protected so you cannot call it directly). (Adding files to java classpath at runtime - Stack Overflow shows how this might be done.)

    With kind regards,

    Konrad

  9. #9
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: class files and classpath or ???

    - You could even use a small hack - you can call the addUrl method of the classloader through reflection (It is protected so you cannot call it directly). (Adding files to java classpath at runtime - Stack Overflow shows how this might be done.)
    This method worked fine to load the necessary classes inside the jar file :D

    Thank you so much :D

    Best Regards
    Tim

Similar Threads

  1. .class Files
    By takugian in forum Advanced Java
    Replies: 0
    Last Post: 11-12-2012, 06:11 PM
  2. Replies: 1
    Last Post: 05-03-2011, 03:28 AM
  3. Replies: 0
    Last Post: 07-13-2010, 07:25 PM
  4. conversion of java .class files to .java files
    By kotturupraveen in forum New To Java
    Replies: 2
    Last Post: 06-09-2008, 01:58 PM
  5. 'class' and 'jar' files
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-29-2007, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •