Click here to Skip to main content
15,892,927 members

Getting methods from a chosen file's class in java

AllainLG asked:

Open original thread
Hi,

I'd like to get all the methods from a file (.text or .java), but I don't know the file's name yet (the user can choose it with jFileChooser). So I don't know the class's name. I have this code:
Java
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());
        }
    }
}


It works, if the class doesn't contains "extends", or uses another class's methods, but if it do, I get errors.
What should I do to fix these problems? I think it has to do something with "classpath" and "bin"
Tags: Java, Reflection

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900