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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:05:01+00:00 2026-06-16T19:05:01+00:00

My issue is that I want to create a class in execution time (I

  • 0

My issue is that I want to create a class in execution time (I create a file .java with the name, package, methods, etc. all right). When I try to compile and execute it always load the old version, not the newer one I write during the execution of my program.

What should I change in my code?

File f = new File("src/pfc/Temp.java");

FileWriter w = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(w);
PrintWriter wr = new PrintWriter(bw);
wr.write("package pfc;\n\npublic class Temp {\n\tpublic float getResult(float[] array){\nfloat res=0;//usar para guardar el resultado final\n");//preparamos la clase
wr.write(entrada.getText());//escribimos en el archivo
wr.write("\nreturn res;\n}\n}");
bw.flush();
bw.close();
wr.flush();
wr.close();
String fileToCompile = f.getAbsolutePath();
try {
    pfc.Temp result = new pfc.Temp();

    //Otra forma

    Class<?> c = Class.forName("pfc.Temp");
    Class[] argTypes = new Class[] { float[].class };
    Method main = c.getDeclaredMethod("getResult", argTypes);
    float nts[] = new float[model.getColumnCount()];
    for(int i = 0; i < model.getRowCount(); i++){
        System.err.println("Voy a la fila: "+i+" de "+model.getRowCount());
        for(int j = 1; j < model.getColumnCount(); j++){
            nts[j] = Float.parseFloat(model.getValueAt(i,j).toString());
            System.err.println("Notas parciales: "+nts[j]+" de la columna "+j+" sobre "+model.getColumnCount());
        }
    }
    Object comp = main.invoke(result, (Object) nts);

Thanks !


Ok, I will try to explain it better. I have a desktop application and I want to load a class when I’m executing it. The problem is that this class I modified with a JTextArea, then I write to the file and want to compile this, but in this way it doesn`t work. It compile the file it was written before I run my application, no the newer I write in the execution.

I will try with ClassLoader, my Temp.java is like this:

package pfc;

public class Temp {
public float getResult(float[] array){
       float res=0;//usar para guardar el resultado final
       float erer = array[1];float fdsfds = array[2];float awd = array[3];

       res= 5;
       return res;
    }
 }

And when I edit this in my aplication, like to replace res= 5; to res= 10; I compile but the result is 5 and not 10.

  • 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-16T19:05:02+00:00Added an answer on June 16, 2026 at 7:05 pm

    I dont understand what does your Temp class, but this code worked for me with a TestClass with a setter and a getter. (You need the tools.jar, you can find it in the JDK filepath \lib )

    package pfc;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class ClassGenerator {
    
    public static BufferedWriter writeClass(String dir, String className) throws IOException {
        BufferedWriter bw1 = new BufferedWriter( new FileWriter( dir + File.separator + className
                + ".java" ) );
        bw1.write( "public class " + className + " {" );
        bw1.newLine( );
        bw1.write( "  private int value;" );
        bw1.newLine( );
        bw1.write( "  public int getValue() {" );
        bw1.newLine( );
        bw1.write( "    return this.value;" );
        bw1.newLine( );
        bw1.write( "  }" );
        bw1.newLine( );
        bw1.write( "  public void setValue(int value) {" );
        bw1.newLine( );
        bw1.write( "    this.value = value;" );
        bw1.newLine( );
        bw1.write( "  }" );
        bw1.newLine( );
        bw1.write( "}" );
        bw1.newLine( );
        bw1.close( );
        return bw1;
    }
    
    public static void main(String [] args) throws IOException, ClassNotFoundException,
            NoSuchMethodException, SecurityException, InstantiationException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        String className = "TestClass";
        String dir = "bin";
        BufferedWriter bw = writeClass( dir , className );
    
        com.sun.tools.javac.Main.compile( new String [] { dir + File.separator + className
                + ".java" } );
    
        ClassLoader classLoader1 = ClassGenerator.class.getClassLoader( );
        Class clazz1 = classLoader1.loadClass( className );
    
        Constructor ctr1 = clazz1.getConstructor( new Class [0] );
        Object testClassInstance = ctr1.newInstance( new Object [0] );
        Method setter1 = clazz1.getMethod( "setValue" , new Class [] { int.class } );
        setter1.invoke( testClassInstance , 12 );
    
        Method getter1 = clazz1.getMethod( "getValue" , new Class [0] );
        Object result = getter1.invoke( testClassInstance );
        System.out.println( result.toString( ) );
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue serializing a c# class to an XML file that
I want to create a class that will mainly house a Vector . The
See here for my issue: http://jsfiddle.net/FvBqA/126/ My issue is that I want to be
The issue I have is that I want to parse strings in order, so
Here's the issue: I have a list of App names that I want to
I'm trying to create remote service that will take care for all client-server communication
I have an issue where inside a class I want to update or add
So I have an issue that I'm not 100% sure on how to solve.
I have an issue that (I think) might have to do with scope, but
I have an issue that is driving me nuts and hoping someone can help.

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.