ファイル (.text または .java) からすべてのメソッドを取得したいのですが、ファイルの名前がまだわかりません (ユーザーは jFileChooser で選択できます)。なのでクラス名はわかりません。私はこのコードを持っています:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import javax.swing.JFileChooser;
public class Test {
public static void main(String[] args) throws Throwable {
JFileChooser fc = new JFileChooser();
File f = null;
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
f = fc.getSelectedFile();
}
int errorCode = com.sun.tools.javac.Main.compile(new String[]{
"-classpath", "bin",
"-d", "../Tmp",
f.getAbsolutePath()});
System.out.println("errorCode:" + errorCode);
File classesDir = new File("../Tmp");
ClassLoader parentLoader = Test.class.getClassLoader();
URLClassLoader loader1 = new URLClassLoader(
new URL[]{classesDir.toURL()}, parentLoader);
BufferedReader br = new BufferedReader(new FileReader(f));
String load = "";
while ((load = br.readLine()) != null) {
if (load.startsWith("package")) {
load = load.replaceAll("package", "") + "." + f.getName().substring(0, f.getName().indexOf("."));
load = load.replace(";", "").trim();
break;
}
}
Class cls1 = loader1.loadClass(load);
Method[] methods = cls1.getDeclaredMethods();
for (Method m : methods) {
System.out.println(m.getName());
}
}
}
クラスに「extends」が含まれていない場合、または別のクラスのメソッドを使用している場合は機能しますが、含まれている場合はエラーが発生します。これらの問題を解決するにはどうすればよいですか? 「クラスパス」と「ビン」で何かをしなければならないと思います