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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:05:16+00:00 2026-06-04T18:05:16+00:00

I am trying to edit a class file and make it available to the

  • 0

I am trying to edit a class file and make it available to the JVM at runtime.
Is that possible, how can I do it?

For example:

main() {
    for (int i=0;i<10;i++) {
        NewClass.method();
        //ask user if to continue..
    }
}

public class NewClass() {
    static void method() {
        sysout("hi");
    }
}

When this loop is executing, I want to change the file NewClass() and load it in the JVM, so that it prints “bye”.

Here is the complete code:

try {
    for (int iCount = 0; iCount < 10; iCount++) {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.print("Enter which method:");
        int i = Integer.parseInt(br.readLine());
        System.out.println(i);
        if (i == 1) {
            Called.calledMethod1();
        } else {
            Called.calledMethod2();
        }
    }
} catch (NumberFormatException nfe) {
    System.err.println("Invalid Format!");
}

I want to run the main method of one class, and while it is running I want to refer to another class, edit it, and refer the 2nd class’s same method and get different output.

I don’t want to:

  1. stop the jvm
  2. edit the code
  3. run the code again.

I want to

  1. edit the code at run time, and get the changes reflected immediately.
  • 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-04T18:05:17+00:00Added an answer on June 4, 2026 at 6:05 pm

    You have to use Java reflection. Using reflection,

    1. you load dynamically a class at runtime using Class forName method.
    2. Using invoke method on the instance of Class you got,you can call any static method you want giving it’s name.

    If your class is fixed at design time, you just skeep the first point. Some code:

    ...
    
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter which class:");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    className=br.readLine()
    int results = compiler.run(null, null, null, className+".java");
    if(results == 0){
    
        Class clazz = Class.forName(className+".class");
        System.out.print("Compiled successfully.Enter which method:");
        Object returnValue=clazz.getMethod(br.readLine()).invoke(null);
    }
    ...
    

    But beware of security issues: the code above let anyone to execute each static method of each class accessible at runtime. If security is one of your concern, it’s better if you first validate the input received from console and test that they match one of the class methods you whant to make available via console.

    EDIT:

    I better understand your question after I read your comment. In order to compile a class at runtime if you are targeting Java 6, you could use Java Compiler object. I have edit the code to include JavaCompiler usage.

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

Sidebar

Related Questions

I'm trying to get edit text to behave in this way: User can input
I'm trying to make a static library from a class but when trying to
I'm trying to create a linked list class in Eclipse but I can't get
I am trying to make HTML code inside my PHP file and return from
Im new to android and i am trying to make a program that outputs
I am trying to access and change a array from a different class file.
I am trying to make a custom Gallery widget. I found that I wanted
I have a container object that is templatized. I am trying to make a
Edit--@Uri correctly pointed out that this was an abuse of annotations; trying to actually
So I'm trying to move my OpenGL code from Main() into a specific class

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.