1

I am trying to make a code to dynamically load a class and use it to create new instances and to run casts. I can make an instance from the newly loaded class using this method: myClass.newInstance();. But I cannot use it as a type. For example: myClass myObj = new myClass(); It doesn't work. Is it possible to perform somehow?

This is the code I was trying to make:

URL classUrl;
classUrl = new URL("file:///C:/classes/");
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
Class c = ucl.loadClass("Operation");
Class MyIn = ucl.loadClass("MyInter"); 
Object o = c.newInstance(); //IT WORKS
System.out.println(((MyIn) o).sum(2, 4)); //IT DOES NOT WORK. Message: MyIn cannot be resolved to a type
3
  • Are you asking why you can't use the class at compile-time, when you've loaded it after compile time? Apr 1, 2016 at 14:30
  • You are right but I would like a way to do it dynamically, loading MyIn dynamically instead putting it into the project Apr 1, 2016 at 14:32
  • MyIn in your code is a variable name and you later on try to cast o to the interface type MyIn - note the same characters here! Change the name of the variable MyIn to something more Java typical like myIn. You could also try to cast o to an instance of MyIn using: MyIn instance = MyIn.getClass().cast(o); and then use it in your application as any other none dynamically loaded instance of MyIn Apr 1, 2016 at 15:11

2 Answers 2

5

Since you don't know MyIn at compile time, you need to use reflection not only to instantiate the class, as you do, but also to call methods.

Something along the lines of:

MyIn.getDeclaredMethod("sum",Integer.TYPE,Integer.TYPE).invoke(o,2,4);

See for example https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html

3
  • 1
    You can call its methods directly if they're in an interface that's both known at compile time and implemented by the dynamically loaded class. Apr 1, 2016 at 14:32
  • MyIn is a inteface that I am trying to load. That's why I am trying to do a cast on o. The internface has an declaration of the method 'sum'. Running the line you gave me, it displays a new error: Caught exception : java.lang.NoSuchMethodException: MyEx.sum() Apr 1, 2016 at 14:47
  • See the API for getDeclaredMethod, needs parameter types. Apr 1, 2016 at 14:56
1

You cannot directly use a class at compile-time that you load later at run-time.

However, you can work directly through one or more interfaces implemented by the loaded classes. Typically the interfaces themselves are included in the compile-time classpath of both the loading code and the loaded code.

The shared interfaces define the expected interaction at compile-time. The loaded code defines a behavior at run-time.

For example, you could load this dynamically loaded class:

public class Operation implements MyInterface { ... }

And then interact with it through a shared interface:

Class c = ucl.loadClass("mypackage.Operation");
Object o = c.newInstance(); //IT WORKS
MyInterface operation = (MyInterface) o;

Now you can interact directly with the methods through the shared interface.

System.out.println( operation.sum(2, 4) );

Constructors

For example: myClass myObj = new myClass(); It doesn't work. Is it possible to perform somehow?

You can create a factory with named constructors in the dynamically loaded code. The factory would also implement a shared interface.

Class cFactory = ucl.loadClass("mypackage.MyFactory");
MyFactoryInterface factory = (MyFactoryInterface) cFactory.newInstance();     
MyInterface myObj = factory.makeOperation( );
8
  • Andy, thanks for the answer. How can I use that interface without putting it into my project? Because, technically, my code doesn't know my newly loaded class has implemented an interface named "MyInterface", right? My code won't be able to see that inteface when I declare it there. This loaded class was also dynamically compiled. And what was generated was just the .class file. I dynamically compiled 'Operation.java' and now I have in my folder 'Operation.java' and 'Operation.class'. I take Operation.class and I load it. But how can I take the interface 'MyInter' that Operation implements? Apr 1, 2016 at 16:17
  • 1
    @AraújoFilho As you have declared the interface as you wrote Operation the interface has to be somewhere available else Operation shouldn't even compile. Usually (like in plug-in-able applications) there is an interface already present in the main code to allow invocation of dynamically loaded classes which have to at least implement the methods specified by the interface else it might get difficult to execute dynamically loaded classes per se Apr 1, 2016 at 16:29
  • Thx @RomanVottner. I got it now. So, is there no way to dynamically load an interface? Apr 1, 2016 at 16:40
  • @AraújoFilho An interface is like any other class in Java. It is loaded the same way also. Though, the interface itself is just a contract for any implementing class to contain a set of methods declared by that interface (if any) So, at runtime you do not really nead the interface but will have to look up the available methods on that object and invoke the one you need. In practice you use interfaces to avoid these reflection methods to retrieve methods, arguments and the like. By including the interface within the dynamically loaded code you do not gain any benefits IMO at least in dynamic CL Apr 1, 2016 at 16:46
  • But if I don't have the interface added into my project I won't be able to dynamically compile that class. What I wanted was to dynamically compile it without needing the interface added into my project. Is that a way to, for example, dynamically load an interface like 'c/classes/MyInterface.class' and use it somehow in my project so that it can be able to compile Operation.java that implements MyInterface? I know how weird it should sounds... Apr 1, 2016 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.