You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassLoaderExecutor.java 1.0KB

1234567891011121314151617181920212223242526272829303132
  1. package com.uqac.rthoni.java_rmi.server.executors;
  2. import com.uqac.rthoni.java_rmi.common.Command;
  3. import com.uqac.rthoni.java_rmi.server.ServerApplication;
  4. import java.io.File;
  5. import java.net.URL;
  6. import java.net.URLClassLoader;
  7. /**
  8. * Created by robin on 9/16/16.
  9. */
  10. public class ClassLoaderExecutor extends AbstractCommandExecutor {
  11. @Override
  12. public String getCommandName() {
  13. return "chargement";
  14. }
  15. @Override
  16. public String run(Command command, ServerApplication server) throws Exception {
  17. String className = command.getArgument(0, false);
  18. String classDir = command.getArgument(1, false);
  19. File root = new File(server.getClassDir(), classDir);
  20. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  21. Class<?> cls = Class.forName(className, true, classLoader); // Should print "hello".
  22. Object instance = cls.newInstance(); // Should print "world".
  23. System.out.println(instance); // Should print "test.Test@hashcode".
  24. return null;
  25. }
  26. }