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 7574895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:32:56+00:00 2026-05-30T16:32:56+00:00

Is there a way to return a non-core Java class to a freshly loaded

  • 0

Is there a way to return a non-core Java class to a freshly loaded state? I want the equivalent of unloading a class and reloading it from scratch. I’m mostly concerned with static initializers and variables.

Problem context: I’m writing a robo-grader for student code. One common student error I’ve seen is to use static variables inappropriately. E.g., consider a Collection with a static count of elements it contains. The collection will work fine the first time it is created and used, but fail on the next instantiation. If I want my tests to be as modular as possible, I need a way to restore clean state after a test.

Right now I’m loading the Class more or less like this, and I’ve sketched how I want to use it.

    String classUnderTest = "package.class";
    Class unitUnderTest;
    try {
        unitUnderTest = Class.forName(classUnderTest);
    } catch (ClassNotFoundException e) {
        System.out.println("Class \"" + classUnderTest + "\" was not found, can't continue.");
        printGradeAndExit();
    }
    // Run foundation tests (stuff on which later tests depend) using unitUnderTest.newInstance()
    runFoundationTests(unitUnderTest);
    // Now reset unitUnderTest for a static variable detection test
    lookForCommonStaticVariableMistakes(unitUnderTest);

Obviously, robo-graders can’t be perfect, but I’d like to catch the common errors, and this is one of them.

According to the Java Language Specification, Section 12.7, supporting unloading of classes is optional (but would do exactly what I want). Is there any way to do this without relying on non-standard features?

The last resort is to do an Ant build which runs a series of tests in separate programs, but I’d like to make this work in a single process if possible.

  • 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-05-30T16:32:58+00:00Added an answer on May 30, 2026 at 4:32 pm

    You don’t necessarily have to unload the class for each run just use a new classloader. You can create a new instance of a URLClassLoader and load your students code using that (as opposed to putting your students code on the regular application classpath). Load the target class using that ClassLoader and then find a method of your choosing using the returned Class object and invoke it. If the method you wish to invoke is non-static you will have to first take a few extra steps to create an instance of the object via reflection.

    Example using a typical public static void main(String[]) method for entry.

    String[] args = new String[0]; //Add arguments for main call.
    
    //Add whatever classpath elements you need.
    String[] classpath = {"file:///example/path/to/studentProjectJarFile.jar", 
                          "file:///example/path/to/studentProjectDir/"};
    
    //Create classloader.
    ClassLoader cl = 
                new URLClassLoader(classpath);
    
    Class<?> mainClazz;
    Method mainMethod;
    
    //Find Class.
    mainClazz = cl.loadClass("com.example.MyClass"); 
    //Find the target method.
    mainMethod = mainClazz.getMethod("main", String[].class); 
    //Invoke method.
    mainMethod.invoke(null, (Object) args); 
    

    In the this example com.example.MyClass should not be on the regular application classpath. If it is then that is the version that will get used instead of the one found by the custom classloader as standard classloaders use a parent-first delegation for loading classes.

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

Sidebar

Related Questions

Is there a way to return a random row from a table using LINQToSQL?
Is there any way to return an array from a function? More specifically, I've
Is there a preferred way to return multiple values from a C++ function? For
Is there a way to manipulate non-global variables from a fileevent handler? Consider the
Is there a way to return a point for a string within a text
Is there a way to return a new version of an array/hash that does
Instead of returning a common string, is there a way to return classic objects?
Is there an easy way to return data to web service clients in JSON
Is there a (simple) way to return the amount of elements colliding with a
I would like to know if there is any way to return an char

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.