Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Reflection, laoding external classes and casting

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Reflection, laoding external classes and casting

    OK here is the meat of the issue:
    1) Start up a java program, inputting a directory
    2) program search directory for .class files
    3) program takes class files and makes a new Object instance of said class
    3a) new objects are sub classes of a file in the program (designed with malice and for thought)
    4) program casts the new objects made from external class fields to the internal super class for usability.

    Here's the challenge: not allowed to use hand coded class loaders.

    I have 1,2,3,3a completed, however any sneaky way i attempt #4 dies against a class not found exception or a class def not found exception. I am a solid java programmer, but the deep dark forest of java meta and reflection is throwing me for a loop. Help in breaking this would be appreciated.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Java Reflection, laoding external classes and casting

    Hello there,

    I would assume you see the class not found or def not found exception because you're trying to load a class that does not exist on the classpath rather than trying to cast it to the internal superclass.

    What does your code look like and have you made sure that the class it is trying to load is actually on the classpath?

  3. The Following User Says Thank You to Json For This Useful Post:

    ashenwolf (May 10th, 2011)

  4. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Reflection, laoding external classes and casting

    dead on the money, striking both of those. looking at the first post i'm leaving out some of the process so I'm going to go over that first.

    I have three files:
    Foo
    Bar
    and Main

    Bar is a subclass of Foo.
    All three are then compiled into .class files
    Bar is then moved to a sub folder in the execution directory.

    the intent and purpose of this exercise is to get modular object loading (low impact coding, on the fly updates etc). I think you might be hitting the head with the Classpath variables... I'm going to investigate this. Below is Main's code (With some things censored out)

    public class Main {
     
        public static void main(String[] args) {
            try {
                Testing t = new Testing();
                ArrayList<Class> test = t.test();
                Object invoke = test.get(0).newInstance();
                Foo foo = (Foo)(invoke);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
     
     
        public ArrayList<Class> test() {
            ArrayList<Class> classList = new ArrayList<Class>();
            File dir = new File("subdir");
            try {
                if (dir.exists()) {
                    ArrayList<File> paths = new ArrayList<File>();
                    ArrayList<String> classes = new ArrayList<String>();
                    for (File f : dir.listFiles()) {
                        if (f.isDirectory()) {
                            paths.add(f);
                        }
                    }
                    paths.add(dir);
                    for (File f : paths) {
                        for (File ff : f.listFiles()) {
                            if (!ff.isDirectory() && ff.getName().endsWith(".class")) {
                                classes.add((f != dir ? f.getName() + "." : "") + ff.getName().substring(0, ff.getName().length() - 6));
                            }
                        }
                    }
                    ClassLoader cl = new URLClassLoader(new URL[]{dir.toURI().toURL()});
                    for (String s : classes) {
                        try {
                            classList.add(cl.loadClass(s));
                        } catch (Exception e) {
                             e.printStackTrace();
                        }
                    }
                }
            } catch (IOException e) {
               e.printStackTrace();
            }
            return classList;
        }
    }

    Bar.class is the only class in the directory subdir. Class path i think is it... if i make Bar extend Foo it chokes inside of the test method with exceptions of class def not found. If i slide an additional Foo inside subdir it pushes the error back to the point of casing in the main method. I will look into the classpath stuff (eep... been far to long since i have messed with that.)

  5. #4
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Reflection, laoding external classes and casting

    Thanks Json, you hit it perfectly, {SOLVED}! Once i added the subdir to the classpath (netbeans here) it ran smooth as ever, and casting worked wonderfully. Excellent, not i just have to work out some minor positioning details and I'm golden. Once again thanks, Json!

Similar Threads

  1. Zipping External Libraries With Jar
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 31st, 2011, 08:49 PM
  2. Running a external exe in Mac OS
    By supertreta in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 15th, 2010, 01:32 PM
  3. Interaction with external application
    By righi in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2010, 08:37 AM
  4. [SOLVED] java Reflection Question - I am lost.
    By prain in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2010, 02:43 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM