Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-dev] Re: Custom Java Project ClassLoader



It is not necessary to create a customer ClassLoader for this purpose you
can use a simple URLClassLoader.  The trick is to get the URL[] that is
needed.  Here is some code from a utility class that we have that creates
this URL[] for a given IJavaProject.

      public static URL[] getClasspathAsURLArray(IJavaProject javaProject)
{
            if (javaProject == null) return null;
            Set visited = new HashSet();
            List urls = new ArrayList(20);
            collectClasspathURLs(javaProject, urls, visited, true);
            URL[] result = new URL[urls.size()];
            urls.toArray(result);
            return result;
      }

      private static void collectClasspathURLs(IJavaProject javaProject,
List urls, Set visited, boolean isFirstProject) {
            if (visited.contains(javaProject)) return;
            visited.add(javaProject);
            IPath outPath =
getJavaProjectOutputAbsoluteLocation(javaProject.getProject());
            outPath = outPath.addTrailingSeparator();
            URL out = createFileURL(outPath);
            urls.add(out);
            IClasspathEntry[] entries = null;
            try {
                  entries = javaProject.getResolvedClasspath(true);
            } catch (JavaModelException e) {
                  return;
            }
            IClasspathEntry entry, resEntry;
            IJavaProject proj = null;
            List projects = null;
            for (int i = 0; i < entries.length; i++) {
                  entry = entries[i];
                  switch (entry.getEntryKind()) {
                        case IClasspathEntry.CPE_LIBRARY :
                        case IClasspathEntry.CPE_CONTAINER :
                        case IClasspathEntry.CPE_VARIABLE :
                              collectClasspathEntryURL(entry, urls);
                              break;
                        case IClasspathEntry.CPE_PROJECT : {
                              if (isFirstProject || entry.isExported())

collectClasspathURLs(getJavaProject(entry), urls, visited, false);

                              break;
                        }
                  }
            }
      }

      private static URL createFileURL(IPath path) {
            URL url = null;
            try {
                  url = new URL("file://" + path.toOSString());
            } catch (MalformedURLException e) {
                  e.printStackTrace();
            }
            return url;
      }


      private static void collectClasspathEntryURL(IClasspathEntry entry,
List urls) {
            URL url = createFileURL(entry.getPath());
            if (url != null)
                  urls.add(url);
      }

      private static IJavaProject getJavaProject(IClasspathEntry entry) {
            IProject proj =
ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().segment(0));
            if (proj != null)
                  return getJavaProject(proj);
            return null;
      }

Hope this helps.

Dan Berg



Wednesday, February 26, 2003 12:08 PM
To: jdt-dev@xxxxxxxxxxx
cc:
From: jdt-dev-request@xxxxxxxxxxx
Subject: jdt-dev digest, Vol 1 #71 - 1 msg



Send jdt-dev mailing list submissions to
jdt-dev@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
http://dev.eclipse.org/mailman/listinfo/jdt-dev
or, via email, send a message with subject or body 'help' to
jdt-dev-request@xxxxxxxxxxx

You can reach the person managing the list at
jdt-dev-admin@xxxxxxxxxxx

When replying, please edit your Subject line so it is more specific
than "Re: Contents of jdt-dev digest..."


Today's Topics:

   1. Accessing a class that exists in the Project classpath from a plugin
   (Cedric ROUVRAIS)

--__--__--

Message: 1
To: jdt-dev@xxxxxxxxxxx
Date: Wed, 26 Feb 2003 16:37:17 +0100 (MET)
From: Cedric ROUVRAIS <crouvrais@xxxxxxxx>
Subject: [jdt-dev] Accessing a class that exists in the Project classpath
from a plugin
Reply-To: jdt-dev@xxxxxxxxxxx

Hi,

I'm in the process of writing a plugin and for various reasons i need to
access
a class that exists in the current project classpath from my plugin.

What i've done in my plugin is this:
Class theFamousClass = Class.forName(theJavaBean);

Where theJavaBean is a class defined in the current project from which the
plugin action has been executed.

However i'm getting a ClassNotFoundException ... i've tried various
workarounds
but i've had no success in getting that class.

Before writing my custom class loader that accesses the output directory of
the
java project to retrieve the file physically i was hoping that there'd be a
cleaner way to do this.

TIA,

Cedric


--__--__--

_______________________________________________
jdt-dev mailing list
jdt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/jdt-dev


 End of jdt-dev Digest



Back to the top