Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java reflection
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:
    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"
Posted 10-Nov-12 14:15pm
Comments
Sergey Alexandrovich Kryukov at 10-Nov-12 22:07pm
   
Errors? You need to tell us exact error messages; in what line, etc.
--SA
AllainLG at 11-Nov-12 6:26am
   
If the file contains "extends":
<pre lang="java">error: cannot find symbol
public class RefactorMuveletek extends MasikOsztaly{</pre>
 
If the input's class is connected to another class:
<pre lang="java">
Exception in thread "main" java.lang.ClassNotFoundException: csomag.eolvasottOsztalyNev
errorCode:1
 
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 DamithSL 225
2 Maciej Los 210
3 Richard MacCutchan 178
4 BillWoodruff 155
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100