示例#1
0
 @SuppressWarnings({"rawtypes", "unchecked", "resource"})
 @Override
 public Object createExecutableExtension(String propertyName) throws CoreException {
   try {
     String className = attributes.get(propertyName);
     IProject project = getProject();
     IJavaProject javaProject = JavaCore.create(project);
     String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
     List<URL> urlList = new ArrayList<URL>();
     for (int i = 0; i < classPathEntries.length; i++) {
       String entry = classPathEntries[i];
       IPath path = new Path(entry);
       URL url = path.toFile().toURI().toURL();
       urlList.add(url);
     }
     ClassLoader parentClassLoader = javaProject.getClass().getClassLoader();
     URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]);
     URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
     ClassLoader cl = classLoader.getParent();
     Class clazz = classLoader.loadClass(className);
     return clazz.getConstructor().newInstance();
   } catch (Exception ex) {
     ex.printStackTrace();
     throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage()));
   }
 }
 private URLClassLoader getProjectClassLoader(final IJavaProject javaProject)
     throws CoreException {
   String[] classPathEntries = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
   List<URL> urlList = new ArrayList<URL>();
   for (int i = 0; i < classPathEntries.length; i++) {
     String entry = classPathEntries[i];
     IPath path = new Path(entry);
     URL url;
     try {
       url = path.toFile().toURI().toURL();
       urlList.add(url);
     } catch (MalformedURLException e) {
       LOGGER.warn("Can't accept URL for '" + path + "': " + e.getMessage());
     }
   }
   ClassLoader parentClassLoader = javaProject.getClass().getClassLoader();
   URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]);
   URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
   return classLoader;
 }