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.