Example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getResolvedClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath.

Prototype

IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;

Source Link

Document

This is a helper method returning the resolved classpath for the project as a list of simple (non-variable, non-container) classpath entries.

Usage

From source file:org.mobicents.eclipslee.servicecreation.util.BaseFinder.java

License:Apache License

/**
 * Searches the specified project's classpath for Jar file containing components.
 * /* ww  w  .j a va2  s  . co  m*/
 * @param projectName
 * @return
 */

private Vector<DTDXML> getComponentsFromClassPath(String projectName) {
    Vector<DTDXML> components = new Vector<DTDXML>();

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);

    IJavaElement ele = JavaCore.create(project);
    if (ele instanceof IJavaProject) {
        IJavaProject javaProject = (IJavaProject) JavaCore.create(project);

        try {
            IClasspathEntry entry[] = javaProject.getResolvedClasspath(true);
            for (int j = 0; j < entry.length; j++) {
                if (entry[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath path = entry[j].getPath();
                    components.addAll(getComponentsFromJar(path));
                }
            }
        } catch (JavaModelException e) {
        }
    }

    return components;
}

From source file:org.mobicents.eclipslee.servicecreation.util.BaseFinder.java

License:Apache License

/**
 * Searches the classpath of every project for component jars.
 * //from  www.  j  a  v  a 2 s.  co m
 * @return
 */

private Vector<DTDXML> getComponentsFromClassPath() {
    Vector<DTDXML> components = new Vector<DTDXML>();

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject projects[] = root.getProjects();

    for (int i = 0; i < projects.length; i++) {
        IJavaElement ele = JavaCore.create(projects[i]);
        if (ele instanceof IJavaProject) {
            IJavaProject project = (IJavaProject) JavaCore.create(projects[i]);

            try {
                IClasspathEntry entry[] = project.getResolvedClasspath(true);
                for (int j = 0; j < entry.length; j++) {
                    if (entry[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath path = entry[j].getPath();
                        components.addAll(getComponentsFromJar(path));
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    }
    return components;
}

From source file:org.mule.munit.plugin.MunitLaunchConfigurationDelegate.java

License:Open Source License

public synchronized void launch(ILaunchConfiguration configuration, String mode, ILaunch launch,
        IProgressMonitor monitor) throws CoreException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }// ww w  .j  a v a2 s.  com
    monitor.beginTask(MessageFormat.format("{0}...", new String[] { configuration.getName() }), 5); //$NON-NLS-1$
    // check for cancellation
    if (monitor.isCanceled()) {
        return;
    }

    try {

        try {
            preLaunchCheck(configuration, launch, new SubProgressMonitor(monitor, 2));
        } catch (CoreException e) {
            if (e.getStatus().getSeverity() == IStatus.CANCEL) {
                monitor.setCanceled(true);
                return;
            }
            throw e;
        }
        // check for cancellation
        if (monitor.isCanceled()) {
            return;
        }

        IProject project = getJavaProject(configuration).getProject();
        project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);

        Map<String, IFolder> sourceFolders = new HashMap<String, IFolder>();
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IJavaProject javaProject = getJavaProject(configuration);
        IPath munitOutputFolder = null;
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath();
                IFolder sourceFolder = root.getFolder(path);
                if (sourceFolder.getLocation().toString().contains("test/munit")) {
                    munitOutputFolder = entry.getOutputLocation();
                }

            }
        }

        MunitEclipseUpdater.launch();

        String mainTypeName = verifyMainTypeName(configuration);
        IVMRunner runner = getVMRunner(configuration, mode);

        File workingDir = verifyWorkingDirectory(configuration);
        String workingDirName = null;
        if (workingDir != null) {
            workingDirName = workingDir.getAbsolutePath();
        }

        String[] envp = getEnvironment(configuration);

        ArrayList vmArguments = new ArrayList();
        ArrayList programArguments = new ArrayList();
        programArguments.add("-resource");
        programArguments.add(configuration.getAttribute("resource", ""));
        programArguments.add("-path");
        programArguments.add(configuration.getAttribute("Mpath", ""));
        programArguments.add("-port");
        programArguments.add(String.valueOf(MunitEclipseUpdater.getInstance().getPort()));

        // VM-specific attributes
        Map vmAttributesMap = getVMSpecificAttributesMap(configuration);

        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath();
                IFolder sourceFolder = root.getFolder(path);
                if (!sourceFolder.getLocation().toString().contains("test/munit")) {
                    try {
                        IFolder folder = root.getFolder(entry.getOutputLocation());
                        for (IResource resource : folder.members()) {
                            try {
                                resource.copy(munitOutputFolder, IFolder.SHALLOW, monitor);
                            } catch (Throwable e) {

                            }
                        }

                    } catch (Throwable y) {

                    }

                }

            }
        }
        String[] classpath = getClasspath(configuration);
        // ClasspathgetC
        List<String> classPathAsList = new ArrayList<String>(Arrays.asList(classpath));
        try {
            URL[] urlClasspath = new ClasspathProvider().getClassPath(getJavaProject(configuration));
            for (URL url : urlClasspath) {
                classPathAsList.add(url.getFile());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        // Create VM config
        VMRunnerConfiguration runConfig = new VMRunnerConfiguration(
                "org.mule.munit.runner.remote.MunitRemoteRunner", classPathAsList.toArray(new String[] {}));
        runConfig.setVMArguments((String[]) vmArguments.toArray(new String[vmArguments.size()]));
        runConfig.setProgramArguments((String[]) programArguments.toArray(new String[programArguments.size()]));
        runConfig.setEnvironment(envp);
        runConfig.setWorkingDirectory(workingDirName);
        runConfig.setVMSpecificAttributesMap(vmAttributesMap);

        // Bootpath
        runConfig.setBootClassPath(getBootpath(configuration));

        // check for cancellation
        if (monitor.isCanceled()) {
            return;
        }

        // done the verification phase
        monitor.worked(1);

        setDefaultSourceLocator(launch, configuration);
        monitor.worked(1);

        runner.run(runConfig, launch, monitor);

        if (monitor.isCanceled()) {
            return;
        }
    } finally {
        fTestElements = null;
        monitor.done();
    }
}

From source file:org.neuro4j.studio.core.util.ClassloaderHelper.java

License:Apache License

private static void collectClasspathURLs(IJavaProject javaProject, List<URL> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;/* w w  w .  j  a  v  a 2  s . c o m*/
    visited.add(javaProject);
    IPath outPath = getJavaProjectOutputAbsoluteLocation(javaProject.getProject());
    if (outPath != null) {
        outPath = outPath.addTrailingSeparator();
        URL out = createFileURL(outPath);
        urls.add(out);

    }

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        return;
    }
    IClasspathEntry entry;
    for (int i = 0; i < entries.length; i++) {
        entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            collectClasspathEntryURL(entry, urls);
            break;
        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;
        }
        }
    }
}

From source file:org.neuro4j.studio.core.util.ClassloaderHelper.java

License:Apache License

private static void collectClasspathIPath(IJavaProject javaProject, List<IPath> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;//from w w  w .j ava2s.com
    visited.add(javaProject);

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        return;
    }
    IClasspathEntry entry;
    for (int i = 0; i < entries.length; i++) {
        entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            addJarToList(entry.getPath(), urls);
            break;
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_SOURCE:
            break;
        case IClasspathEntry.CPE_VARIABLE:

            addJarToList(entry.getPath(), urls);
            break;
        case IClasspathEntry.CPE_PROJECT: {
            if (isFirstProject || entry.isExported())
                collectClasspathIPath(getJavaProject(entry), urls, visited, false);
            break;
        }
        }
    }
}

From source file:org.objectstyle.wolips.eomodeler.eclipse.EclipseEOModelGroupFactory.java

License:Open Source License

protected void addModelsFromProject(EOModelGroup modelGroup, IProject project, Set<Object> searchedResources,
        Set<IProject> searchedProjects, Set<EOModelVerificationFailure> failures, boolean skipOnDuplicates,
        IProgressMonitor progressMonitor, int depth) throws IOException, EOModelException, CoreException {
    if (!searchedProjects.contains(project)) {
        progressMonitor.setTaskName("Adding models from " + project.getName() + " ...");
        searchedProjects.add(project);//from ww w  . j ava  2  s  .c o m
        if (!project.exists()) {
            failures.add(new EOModelVerificationFailure(null,
                    "The dependent project '" + project.getName() + "' does not exist.", false));
        } else if (!project.isOpen()) {
            failures.add(new EOModelVerificationFailure(null,
                    "The dependent project '" + project.getName() + "' exists but is not open.", false));
        } else {
            boolean visitedProject = false;
            boolean isJavaProject = project.getNature(JavaCore.NATURE_ID) != null;
            IClasspathEntry[] classpathEntries = null;
            if (isJavaProject) {
                IJavaProject javaProject = JavaCore.create(project);
                classpathEntries = javaProject.getResolvedClasspath(true);
            } else {
                classpathEntries = new IClasspathEntry[0];
            }
            boolean showProgress = (depth == 0);
            if (showProgress) {
                progressMonitor.beginTask("Scanning " + project.getName() + " classpath ...",
                        classpathEntries.length + 1);
            }
            for (int classpathEntryNum = 0; classpathEntryNum < classpathEntries.length; classpathEntryNum++) {
                IClasspathEntry entry = classpathEntries[classpathEntryNum];
                int entryKind = entry.getEntryKind();
                if (entryKind == IClasspathEntry.CPE_LIBRARY) {
                    List<IPath> jarPaths = new LinkedList<IPath>();
                    IPath path = entry.getPath();
                    IPath frameworkPath = null;
                    while (frameworkPath == null && path.lastSegment() != null) {
                        String lastSegment = path.lastSegment();
                        if (lastSegment != null && lastSegment.endsWith(".framework")) {
                            frameworkPath = path;
                        } else {
                            if (lastSegment != null && lastSegment.endsWith(".jar")) {
                                // MS: This is really annoying, but it appears that a jar in your project looks the
                                // same as an absolute jar path reference outside your project.  I don't know
                                // how to tell them apart, so I check to see if the jar is in your project 
                                // before we fallback to the old way.
                                IFile jarInProject = project.getWorkspace().getRoot().getFile(path);
                                if (jarInProject.exists()) {
                                    jarPaths.add(jarInProject.getLocation());
                                } else {
                                    jarPaths.add(path);
                                }
                            }
                            path = path.removeLastSegments(1);
                        }
                    }

                    if (frameworkPath != null) {
                        File resourcesFolder = frameworkPath.append("Resources").toFile();
                        if (!searchedResources.contains(resourcesFolder) && resourcesFolder.exists()) {
                            searchedResources.add(resourcesFolder);
                            modelGroup.loadModelsFromURL(resourcesFolder.toURL(), 1, failures, skipOnDuplicates,
                                    progressMonitor);
                        }
                    }

                    for (IPath jarPath : jarPaths) {
                        URL jarResourcesURL = new URL("jar:" + jarPath.toFile().toURL() + "!/Resources");
                        if (!searchedResources.contains(jarResourcesURL) && URLUtils.exists(jarResourcesURL)) {
                            modelGroup.loadModelsFromURL(jarResourcesURL, 1, failures, skipOnDuplicates,
                                    progressMonitor);
                        }
                    }
                } else if (entryKind == IClasspathEntry.CPE_PROJECT) {
                    IPath path = entry.getPath();
                    IProject dependsOnProject = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(path.lastSegment());
                    addModelsFromProject(modelGroup, dependsOnProject, searchedResources, searchedProjects,
                            failures, skipOnDuplicates, progressMonitor, depth + 1);
                } else if (entryKind == IClasspathEntry.CPE_SOURCE) {
                    visitedProject = true;
                    project.accept(new ModelVisitor(project, modelGroup, searchedResources, failures,
                            skipOnDuplicates, progressMonitor), IResource.DEPTH_INFINITE,
                            IContainer.EXCLUDE_DERIVED);
                }
                if (showProgress) {
                    progressMonitor.worked(1);
                }
            }

            if (!visitedProject) {
                project.accept(new ModelVisitor(project, modelGroup, searchedResources, failures,
                        skipOnDuplicates, progressMonitor), IResource.DEPTH_INFINITE,
                        IContainer.EXCLUDE_DERIVED);
                if (showProgress) {
                    progressMonitor.worked(1);
                }
            }
        }
    }
}

From source file:org.objectstyle.wolips.jrebel.utils.WOProjectClassLoader.java

License:BSD License

private void addURLs(IJavaProject javaProject, boolean exportsOnly) {
    if (!javaProjects.contains(javaProject)) {
        javaProjects.add(javaProject);//  www.  j a v a 2s.  c o m

        try {
            // Add default output location
            addURL(javaProject.getOutputLocation());

            // Add each classpath entry
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            for (IClasspathEntry classpathEntry : classpathEntries) {
                if (classpathEntry.isExported() || !exportsOnly) {
                    switch (classpathEntry.getEntryKind()) {

                    // Recurse on projects
                    case IClasspathEntry.CPE_PROJECT:
                        IProject project = javaProject.getProject().getWorkspace().getRoot()
                                .getProject(classpathEntry.getPath().toString());
                        IJavaProject javaProj = JavaCore.create(project);
                        if (javaProj != null) {
                            addURLs(javaProj, true);
                        }
                        break;

                    // Library
                    case IClasspathEntry.CPE_LIBRARY:
                        addURL(classpathEntry);
                        break;

                    // Only Source entries with custom output location need to be added
                    case IClasspathEntry.CPE_SOURCE:
                        IPath outputLocation = classpathEntry.getOutputLocation();
                        if (outputLocation != null) {
                            addURL(outputLocation);
                        }
                        break;

                    // Variable and Container entries should not be happening, because
                    // we've asked for resolved entries.
                    case IClasspathEntry.CPE_VARIABLE:
                    case IClasspathEntry.CPE_CONTAINER:
                        break;
                    }
                }
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
            // log.debug("MalformedURLException occurred: " + e.getLocalizedMessage(),e);
        }
    }
}

From source file:org.openoffice.ide.eclipse.java.JavaBuilder.java

License:LGPL

/**
 * {@inheritDoc}//from w  ww  .j a  va 2s. com
 */
public String[] getBuildEnv(IUnoidlProject pUnoProject) {

    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoProject.getName());

    String[] env = new String[2];

    // compute the classpath for the project's OOo instance
    String classpath = "CLASSPATH="; //$NON-NLS-1$
    String sep = System.getProperty("path.separator"); //$NON-NLS-1$

    File javaHomeFile = null;

    // Compute the classpath for the project dependencies
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
        try {
            IClasspathEntry[] cpEntry = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < cpEntry.length; i++) {
                IClasspathEntry entry = cpEntry[i];

                // Transform into the correct path for the entry.
                if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                    classpath += entry.getPath().toOSString();
                }
                if (i < cpEntry.length - 1) {
                    classpath += sep;
                }
            }

            IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
            javaHomeFile = vmInstall.getInstallLocation();

        } catch (JavaModelException e) {
            PluginLogger.error(Messages.getString("Language.GetClasspathError"), e); //$NON-NLS-1$
        } catch (CoreException e) {
            // TODO log a problem to find the JVM associated to the project
        }
    }

    env[0] = classpath;
    if (javaHomeFile != null) {
        String libs = ""; //$NON-NLS-1$
        String filesep = System.getProperty("file.separator"); //$NON-NLS-1$
        try {
            String arch = System.getProperty("os.arch"); //$NON-NLS-1$
            libs = javaHomeFile.getCanonicalPath() + filesep + "lib" + filesep + arch; //$NON-NLS-1$
        } catch (IOException e) {
        }
        env[1] = "LD_LIBRARY_PATH=" + libs; //$NON-NLS-1$
    }

    return env;
}

From source file:org.openoffice.ide.eclipse.java.JavaBuilder.java

License:LGPL

/**
 * Get the libraries in the classpath that are located in the project
 * directory or one of its subfolder.// w  ww .jav  a 2  s  .c o m
 *
 * @param pJavaPrj the project from which to extract the libraries
 * @return a list of all the File pointing to the libraries.
 */
private ArrayList<IFile> getLibs(IJavaProject pJavaPrj) {
    ArrayList<IFile> libs = new ArrayList<IFile>();
    try {
        IClasspathEntry[] entries = pJavaPrj.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                /*
                 * At first, add only the libraries located in the project
                 * or one of its children. All others libraries have to be
                 * managed y the user.
                 */
                IPath path = entry.getPath();
                if (!new File(path.toOSString()).exists() && path.isAbsolute()
                        && path.toString().startsWith("/" + pJavaPrj.getProject().getName())) { //$NON-NLS-1$
                    // Relative to the project
                    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
                    if (file != null && file.exists()) {
                        libs.add(file);
                    }
                }
            }
        }

    } catch (JavaModelException e) {
        // Enable to add some missing library
    }

    return libs;
}

From source file:org.rascalmpl.eclipse.nature.ProjectEvaluatorFactory.java

License:Open Source License

private void collectClassPathForProject(IProject project, List<URL> classPath, List<String> compilerClassPath,
        Evaluator parser) {//from  w w w.  j a  v  a2s .c  o m
    try {
        if (!project.hasNature(JavaCore.NATURE_ID)) {
            for (IProject ref : project.getReferencedProjects()) {
                collectClassPathForProject(ref, classPath, compilerClassPath, parser);
            }
        } else {
            IJavaProject jProject = JavaCore.create(project);

            IPath binFolder = jProject.getOutputLocation();
            String binLoc = project.getLocation() + "/" + binFolder.removeFirstSegments(1).toString();
            compilerClassPath.add(binLoc);

            URL binURL = new URL("file", "", binLoc + "/");
            parser.addClassLoader(new URLClassLoader(new URL[] { binURL }, getClass().getClassLoader()));
            classPath.add(binURL);

            if (!jProject.isOpen()) {
                return;
            }
            IClasspathEntry[] entries = jProject.getResolvedClasspath(true);

            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                switch (entry.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    if (entry.getPath().segment(0).equals(project.getName())) {
                        String file = project.getLocation() + "/"
                                + entry.getPath().removeFirstSegments(1).toString();
                        URL url = new URL("file", "", file);
                        if (!classPath.contains(url)) {
                            classPath.add(url);
                            compilerClassPath.add(file);
                        }
                    } else {
                        URL url = new URL("file", "", entry.getPath().toString());
                        if (!classPath.contains(url)) {
                            classPath.add(url);
                            compilerClassPath.add(entry.getPath().toString());
                        }
                    }
                    break;
                case IClasspathEntry.CPE_PROJECT:
                    collectClassPathForProject(
                            (IProject) project.getWorkspace().getRoot().findMember(entry.getPath()), classPath,
                            compilerClassPath, parser);
                    break;
                }
            }
        }
    } catch (CoreException e) {
        Activator.getInstance().logException("failed to configure classpath", e);
    } catch (MalformedURLException e) {
        Activator.getInstance().logException("failed to configure classpath", e);
    }
}