我的问题是我想在执行时创建一个类(我创建一个文件.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;到res = 10;我编译但结果是5而不是10。
答案 0 :(得分:1)
实际上你需要使用ClassLoader来加载类。另外,如果您只是使用.java文件,则需要编译以生成.class文件以便加载,然后调用构造函数来获取实例。
Here有一个与该主题相关的有趣博客
答案 1 :(得分:1)
我不明白你的Temp类是什么,但是这个代码对我有一个带有setter和getter的TestClass。 (您需要tools.jar,您可以在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( ) );
}
}