gpt4 book ai didi

Java动态类加载器

转载 作者:行者123 更新时间:2023-11-30 04:33:13 26 4
gpt4 key购买 nike

我的问题是我想在执行时创建一个类(我创建一个文件.java,其中包含名称、包、方法等)。当我尝试编译并执行它时,它总是加载旧版本,而不是我在程序执行期间编写的新版本。

我应该更改代码中的哪些内容?

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);

谢谢!

<小时/>

好的,我会尽力解释得更好。我有一个桌面应用程序,我想在执行它时加载一个类。问题是我用 JTextArea 修改了这个类,然后我写入文件并想要编译它,但这样它不起作用。它编译我运行应用程序之前编写的文件,而不是我在执行中编写的较新文件。

我会尝试使用ClassLoader,我的Temp.java是这样的:

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;
}
}

当我在应用程序中编辑它时,喜欢替换 res= 5;至分辨率= 10;我编译了,但结果是 5 而不是 10。

最佳答案

我不明白你的 Temp 类的作用是什么,但是这段代码对我来说适用于带有 setter 和 getter 的 TestClass。 (您需要tools.jar,您可以在JDK文件路径\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( ) );
}
}

关于Java动态类加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14119225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com