Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:01:10+00:00 2026-06-18T07:01:10+00:00

I trying to get experience with dynamic class loading in Java. So any comments

  • 0

I trying to get experience with dynamic class loading in Java. So any comments and help are welcome. I have a program that allow the user to select a file and do some actions on it. The actions are “Commands”, those are the class I try to load.

The way it works is : the user put a .class file a the desired folder, my program checks the files in the folder and if there’s a class in a .class file, it loads it.

I did it, but not as I wanted. For now, it works only with the classes that have been compiled with my program. But what I want is that I could put any .class file that contains a class in the folder and my program loads it. That’s my code for now :

for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].endsWith(".class")) {
        /////MY FIRT TRY/////ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
        ClassLoader classLoader = FileMod.class.getClassLoader();

        // Define a class to be loaded.
        String classNameToBeLoaded = fileList[i].replace(".class", "");

        // Load the class
        try {
            /////MY FIRST TRY/////Class myClass = myClassLoader.loadClass(classNameToBeLoaded);
            //if the class exists in the file
            Class aClass = classLoader.loadClass(classNameToBeLoaded);
            classList.add(aClass);
            System.out.println("CLASS FOUND : " + classNameToBeLoaded + aClass.getSuperclass());
        } catch (ClassNotFoundException e) {
            System.out.println("CLASS NOT FOUND : " + classNameToBeLoaded);
            continue;
        }
    }
}

As you can see, I have tried two ways, the first one is currently in comments. What I do is checking every file in the folder and check if it’s a .class file, if yes I try to load the class if there is one. I guess that the two classLoaders can only load the files they “know”, so how Could I load a external class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:01:11+00:00Added an answer on June 18, 2026 at 7:01 am

    You generally need a new class loader. Use java.net.URLClassLoader.newInstance. Careful though, you are now loading classes from outside into your application.

    (Some class loaders will allow you to add locations, but that’s a real hack.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK so I'm trying to get started with Xcode and I have some experience
I don't have much HTML/CSS experience, but I am trying to get a simple
I'm trying to get the facebook comments box to integrate with a microsite that
I have some experience with NHibernate and I'm trying to get started with Castle
I am trying to get some experience with chrome extensions. I have a question
I'm trying to get familiar with Git having only SVN prior experience. I have
I have no experience in prestashop. I'm trying to get all products which matches
I was wondering if anyone had experience of trying to get Eth to enter
all. We're trying to get some intersect collisions working, but the problem experience is
Trying to get a wildcard search to pick up on any text in org_name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.