Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Is it possible to load classes at runtime that are discovered from a directory

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to load classes at runtime that are discovered from a directory

    I'm working on a command shell program. The idea is it's a blend between the AS/400 and a unix shell with dropdown autocompletes, syntax highlighting, etc. One feature I'd like to implement is a way for users to create their own classes for additional functionality. They would design their classes off of a standard superclass, compile them and then drop them in the classes directory. The program would look in the directory at runtime and load them. Is this possible with my program not knowing the class names before it's compiled?


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    File Class Loader Java Tips Weblog

    db

    edit That's a link
    Last edited by Darryl.Burke; June 6th, 2010 at 07:56 AM.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Thanks I'm checking it out now.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Yes, in some forum software I've written I've added the ability to drop a jar file into a plugin directory and then load that during runtime. You just need to use reflection really.

    1. Open the JAR file
    2. Loop through all the entries in it
    3. If an entry ends with .class, do Class.forName with its fully qualified package name and you're off

    // Json

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    What if the .class files are NOT on the classpath. Will Class.forName() work?
    I think you need your own class loader to read the bytes from the .class file and make a class from it.

  6. #6
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Quote Originally Posted by Norm View Post
    What if the .class files are NOT on the classpath. Will Class.forName() work?
    I think you need your own class loader to read the bytes from the .class file and make a class from it.
    That's what File Class Loader does.

    db

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Sorry, I missed that your earlier post was a link. Your post was a bit too subtle.

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    I just used the URLClassLoader (Java Platform SE 6)

    final URL[] urls = { new URL("file", null, file.getAbsolutePath()) };
    final URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());

    Something like that

    // Json

  9. #9
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Quote Originally Posted by Norm View Post
    Sorry, I missed that your earlier post was a link. Your post was a bit too subtle.
    I'd rather say the (lack of special) formatting for links is (more than) a bit too subtle.

    db

  10. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Quote Originally Posted by Darryl.Burke View Post
    I'd rather say the (lack of special) formatting for links is (more than) a bit too subtle.

    db
    I've raised this as an issue and hopefully we'll be able to resolve that.

    // Json

  11. The Following User Says Thank You to Json For This Useful Post:

    Darryl.Burke (June 9th, 2010)

  12. #11
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Quote Originally Posted by Json View Post
    I've raised this as an issue and hopefully we'll be able to resolve that.

    // Json
    Thank you Json.

    db

  13. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Is it possible to load classes at runtime that are discovered from a directory

    Quote Originally Posted by Json View Post
    I've raised this as an issue and hopefully we'll be able to resolve that.
    A workaround is to underscore (U) the link text, but it's easy to forget...

Similar Threads

  1. Create buttons at runtime
    By rtumatt in forum AWT / Java Swing
    Replies: 2
    Last Post: May 24th, 2010, 06:42 AM
  2. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM
  3. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 03:52 AM
  4. How to Navigate to a URL with Runtime.getRuntime().exec()
    By Flash in forum Java SE API Tutorials
    Replies: 4
    Last Post: October 5th, 2009, 07:18 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM