Hi, a need help for a quite sophisticated thing, I explain: a want to write a program that would use user-created classes; the user would be asked the name of the file where the class is (either code or bytecode) and the program would use that class . The problem is that the program is complied and running before knowing about that classes. I dont know if this can be done in Java. If someone knows how to do this or is sure that this cannot be done in Java, please let me know. Thanks, Xabi.
require the classes to be used have implements the JMX interface, your server program has access to the MBean configuration file which curtains the class name, service name and location. maybe I can't express it clearly, your can have a look on JMX standard.
Hi... Like someone says, take a look to reflection. You can ask from the user a abc.class (already compiled class). Use the Class.forName("abc") to "load" the desired class and then use the class.newInstace() to get an instance for this class. You better make the user to implement an interface you made so when you create the new instace you can call the methods from the interface you made. example: 1. make an interface: --->public interface KUKU { --- void myMethod(); --- } * Take the class from the user... 2. 'load' the class by: ----> Class cl = Class.forName("userClassName"); 3. create an instance of it: ----> Object obj = cl.newInstance(); 4. cast the object to the interface you create ----> KUKU k = (KUKU)obj; 5. use it as: ----> k.myMethod();
your are actually calling the myMethod of the user class
Once you import the class then you would have to tell the programme exactly how to use this class therefore would have to add code to this programme that is all ready compiled. The compiled programme will not use this new class unless told exactly how to use it(through the use of code) hence you will eventually have to recompile it. I might be missing something but I don't see any purpose in having a running programme add a new class to it. I am no expert but am in my third year of university so could you explain why you would want to import a programme once it is compiled then the programme will start using the class without any additional code? To me this don't make sense but I could be missing the purpose from lack of experience?