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

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

Introduction

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

Prototype

void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the classpath of this project using a list of classpath entries.

Usage

From source file:com.microsoft.azure.hdinsight.projects.CreateProjectUtil.java

License:Open Source License

private static void createResourceStructForLocalRunScalaProject(IFolder sourceRootFolder, String rootPath,
        IProject project) throws CoreException {
    copyFileTo(Scala_Local_Run_Sample, rootPath);
    final IFolder dataFolder = sourceRootFolder.getParent().getFolder(new Path("data"));
    if (!dataFolder.exists()) {
        dataFolder.create(false, true, null);
    }/*from  w w w .  j  a v  a  2  s.c o  m*/
    copyFileTo(Scala_Local_Run_Sample_Data, dataFolder.getLocation().toFile().getAbsolutePath());

    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries = javaProject.getRawClasspath();

    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
    System.arraycopy(entries, 0, newEntries, 0, entries.length);

    IPath dataPath = javaProject.getPath().append("data");
    IClasspathEntry dataEntry = JavaCore.newSourceEntry(dataPath, null);

    newEntries[entries.length] = JavaCore.newSourceEntry(dataEntry.getPath());
    javaProject.setRawClasspath(newEntries, null);
}

From source file:com.microsoft.azuretools.wasdkjava.ui.classpath.ClasspathContainerPage.java

License:Open Source License

/**
 * Method adds entries into .classpath file.
 *//*from   w  ww.ja v a2  s  .  c  o m*/
private void configureClasspathEntries() {
    IJavaProject proj1 = JavaCore.create(getSelectedProject());
    IClasspathEntry[] entries;
    try {
        entries = proj1.getRawClasspath();
        IClasspathEntry[] newentries = new IClasspathEntry[entries.length];

        for (int i = 0; i < entries.length; i++) {
            if (entries[i].toString().contains(Messages.containerID)) {
                if (depCheck.getSelection()) {
                    IClasspathAttribute[] attr = new IClasspathAttribute[1];
                    attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
                    newentries[i] = JavaCore.newContainerEntry(entry, null, attr, true);
                } else {
                    newentries[i] = JavaCore.newContainerEntry(entry);
                }
            } else {
                newentries[i] = entries[i];
            }
        }
        proj1.setRawClasspath(newentries, null);
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}

From source file:com.microsoft.sqljdbc4.ui.classpath.ClasspathContainerPage.java

License:Open Source License

/**
 * Method adds entries into .classpath file.
 *///from w  w w .j a v  a  2  s. c o  m
private void configureClasspathEntries() {
    IJavaProject proj1 = JavaCore.create(getSelectedProject());
    IClasspathEntry[] entries;
    try {
        entries = proj1.getRawClasspath();
        IClasspathEntry[] newentries = new IClasspathEntry[entries.length];

        for (int i = 0; i < entries.length; i++) {
            if (entries[i].toString().contains(Messages.sdkContainer)) {
                if (depCheck.getSelection()) {
                    IClasspathAttribute[] attr = new IClasspathAttribute[1];
                    attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
                    newentries[i] = JavaCore.newContainerEntry(entry, null, attr, true);

                } else {
                    newentries[i] = JavaCore.newContainerEntry(entry);
                }
            } else {
                newentries[i] = entries[i];
            }
        }
        proj1.setRawClasspath(newentries, null);
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}

From source file:com.microsoftopentechnologies.acsfilter.ui.classpath.ClasspathContainerPage.java

License:Open Source License

/**
 * Method adds ACS filter and filter mapping tags in web.xml
 * and saves input values given on ACS library page.
 * In case of edit, populates previously set values.
 *///  ww w . ja  va 2  s  .  c  om
private void configureDeployment() {
    //edit library
    if (isEdit()) {
        IJavaProject proj1 = JavaCore.create(ACSFilterUtil.getSelectedProject());
        IClasspathEntry[] entries;
        try {
            entries = proj1.getRawClasspath();
            IClasspathEntry[] newentries = new IClasspathEntry[entries.length];

            for (int i = 0; i < entries.length; i++) {
                if (entries[i].toString().contains(Messages.sdkContainer)) {
                    if (depCheck.getSelection()) {
                        IClasspathAttribute[] attr = new IClasspathAttribute[1];
                        attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
                        newentries[i] = JavaCore.newContainerEntry(entry, null, attr, true);
                    } else {
                        newentries[i] = JavaCore.newContainerEntry(entry);
                    }
                } else {
                    newentries[i] = entries[i];
                }
            }
            proj1.setRawClasspath(newentries, null);
        } catch (Exception e) {
            Activator.getDefault().log(e.getMessage(), e);
        }
    }

    ACSFilterHandler handler = null;
    try {
        IProject proj = ACSFilterUtil.getSelectedProject();
        if (proj.getFile(xmlPath).exists()) {
            handler = new ACSFilterHandler(proj.getFile(xmlPath).getLocation().toOSString());
            handler.setAcsFilterParams(Messages.acsAttr, acsTxt.getText());
            handler.setAcsFilterParams(Messages.relAttr, relTxt.getText());
            if (!embedCertCheck.getSelection()) {
                handler.setAcsFilterParams(Messages.certAttr, certTxt.getText());
                if (getEmbeddedCertInfo() != null)
                    removeEmbedCert(ACSFilterUtil.getSelectedProject());
            } else {
                handler.removeParamsIfExists(Messages.certAttr);
                if (!certTxt.getText().isEmpty()) {
                    String srcLoc = ACSFilterUtil.getSelectedProject().getFolder(certificateLocation)
                            .getLocation().toOSString();
                    String certLoc = String.format("%s%s%s", srcLoc, File.separator, Messages.acsCertLoc);
                    File destination = new File(certLoc);
                    if (!destination.getParentFile().exists())
                        destination.getParentFile().mkdir();
                    copy(new File(CerPfxUtil.getCertificatePath(certTxt.getText())), destination);
                }
            }
            handler.setAcsFilterParams(Messages.secretKeyAttr, generateKey());
            handler.setAcsFilterParams(Messages.allowHTTPAttr,
                    requiresHttpsCheck.getSelection() ? "false" : "true");
        } else {
            boolean choice = MessageDialog.openQuestion(this.getShell(), Messages.depDescTtl,
                    Messages.depDescMsg);
            if (choice) {
                String path = createWebXml(depDirLoc);
                //copy cert into WEB-INF/cert/_acs_signing.cer location if embed cert is selected 
                if (embedCertCheck.getSelection()) {
                    String srcLoc = ACSFilterUtil.getSelectedProject().getFolder(certificateLocation)
                            .getLocation().toOSString();
                    String certLoc = String.format("%s%s%s", srcLoc, File.separator, Messages.acsCertLoc);
                    File destination = new File(certLoc);
                    if (!destination.getParentFile().exists())
                        destination.getParentFile().mkdir();
                    copy(new File(CerPfxUtil.getCertificatePath(certTxt.getText())), destination);
                }
                handler = new ACSFilterHandler(path);
                handler.setAcsFilterParams(Messages.acsAttr, acsTxt.getText());
                handler.setAcsFilterParams(Messages.relAttr, relTxt.getText());
                if (!embedCertCheck.getSelection()) { //Donot make entry if embed cert is selected
                    handler.setAcsFilterParams(Messages.certAttr, certTxt.getText());
                    if (getEmbeddedCertInfo() != null)
                        removeEmbedCert(ACSFilterUtil.getSelectedProject());
                }
                handler.setAcsFilterParams(Messages.secretKeyAttr, generateKey());
                handler.setAcsFilterParams(Messages.allowHTTPAttr,
                        requiresHttpsCheck.getSelection() ? "false" : "true");
            } else {
                finishVal = true;
                return;
            }
        }
    } catch (Exception e) {
        MessageDialog.openError(this.getShell(), Messages.acsErrTtl, Messages.acsErrMsg);
        finishVal = false;
        Activator.getDefault().log(e.getMessage(), e);
    }
    try {
        handler.save();
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        root.refreshLocal(IResource.DEPTH_INFINITE, null);
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
        MessageDialog.openError(this.getShell(), Messages.acsErrTtl, Messages.saveErrMsg);
        finishVal = false;
    }
}

From source file:com.motorola.studio.android.common.utilities.EclipseUtils.java

License:Apache License

/**
 * This method adds a list of paths to all projects classpaths settings.
 * @param javaProjects List of projects that will have the classpath changed
 * @param libsPaths List of lib paths to be added to Projects' classpaths
 * @param monitor Monitor to track progress or null if it's not necessary.
 * @return IStatus The status of the operation. This method stops processing at the first error found.
 *//*from   w  w  w. j a v  a 2s.com*/
public static IStatus addLibsToProjects(List<IJavaProject> javaProjects, List<IPath> libsPaths,
        IProgressMonitor monitor) {
    SubMonitor subMonitor = SubMonitor.convert(monitor);
    subMonitor.beginTask(UtilitiesNLS.ProjectUtils_AddLibsProgress_ConfiguringClassPaths,
            ((javaProjects.size() * 2) + libsPaths.size()) * 1000);
    IStatus status = Status.OK_STATUS;
    IClasspathEntry[] classPathEntries = new IClasspathEntry[libsPaths.size()];
    int i = 0;
    subMonitor.subTask(UtilitiesNLS.ProjectUtils_AddLibsProgress_PreparingPaths);
    for (IPath libPath : libsPaths) {
        IClasspathEntry classpathEntry = JavaCore.newLibraryEntry(libPath, null, null);
        classPathEntries[i] = classpathEntry;
        i++;
        subMonitor.worked(1000);
    }

    subMonitor.subTask(UtilitiesNLS.ProjectUtils_AddLibsProgress_ConfiguringProjects);
    for (IJavaProject javaProject : javaProjects) {
        IClasspathEntry[] rawClasspath;
        try {
            rawClasspath = javaProject.getRawClasspath();
            int length = rawClasspath.length;
            int newEntriesLength = classPathEntries.length;
            int newLenght = length + newEntriesLength;
            IClasspathEntry[] newClassPath = new IClasspathEntry[newLenght];

            System.arraycopy(rawClasspath, 0, newClassPath, 0, length); //Copy the existent classPath to the new array.
            System.arraycopy(classPathEntries, 0, newClassPath, length, newEntriesLength); //Copy the new entries to the new array
            subMonitor.worked(1000);
            javaProject.setRawClasspath(newClassPath, subMonitor.newChild(1000)); // Set the Project's classpath.
        } catch (JavaModelException e) {
            status = new Status(IStatus.ERROR, CommonPlugin.PLUGIN_ID,
                    UtilitiesNLS.ProjectUtils_AddLibsProgress_ErrorSettingClasspaths, e);
            break;
        }
    }
    subMonitor.done();
    return status;
}

From source file:com.motorola.studio.android.model.ProjectCreationSupport.java

License:Apache License

/**
 * Setup src folders/*from   w ww  .j av a2s . co  m*/
 * @param javaProject
 * @param sourceFolder
 * @param monitor
 * @throws JavaModelException
 */
private static void setupSourceFolders(IJavaProject javaProject, List<String> sourceFolders,
        IProgressMonitor monitor) throws JavaModelException {
    monitor.beginTask(AndroidNLS.UI_ProjectCreationSupport_Preparing_Source_Folders_Task,
            (sourceFolders.size() * 100) + 100);
    try {
        IProject project = javaProject.getProject();
        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (String sourceFolder : sourceFolders) {
            IFolder srcFolder = project.getFolder(sourceFolder);
            entries = removeClasspathEntry(entries, srcFolder);
            entries = removeClasspathEntry(entries, srcFolder.getParent());
            entries = ProjectUtils.addEntryToClasspath(entries,
                    JavaCore.newSourceEntry(srcFolder.getFullPath()));
            monitor.worked(100);
        }

        javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 100));
    } finally {
        monitor.done();
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

/**
 * For this marvelous project we need to: - create the default Eclipse
 * project - add the custom project nature - create the folder structure
 * /* w  ww  .  ja va 2 s.  com*/
 * @param projectName
 * @param location
 * @param natureId
 * @return
 */
public static IProject createProject(String projectName, URI location) {
    Assert.isNotNull(projectName);
    Assert.isTrue(projectName.trim().length() > 0);

    try {
        // create eclipse project
        IProject project = createBaseProject(projectName, location);
        project.setDefaultCharset("UTF-8", null);
        addNature(project);
        // create java project
        IJavaProject javaProject = JavaCore.create(project);
        javaProject.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);

        // add bin/ouput folder
        IFolder binFolder = project.getFolder("bin");
        // binFolder.create(false, true, null);
        javaProject.setOutputLocation(binFolder.getFullPath(), null);

        // add libs to project class path
        List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();

        // create source folder
        IFolder sourceFolder = project.getFolder("src");
        sourceFolder.create(false, true, null);
        IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] cEntries = new IClasspathEntry[3];
        cEntries[0] = JavaRuntime.getDefaultJREContainerEntry();
        // cEntries[2] = JavaCore.new
        cEntries[1] = JavaCore.newSourceEntry(srcRoot.getPath());
        cEntries[2] = JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"));
        javaProject.setRawClasspath(cEntries, null);
        /*
         * Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name:
         * LePlugine Bundle-SymbolicName: LePlugine Bundle-Version:
         * 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.7
         */
        String[] paths = { "transformations/T2M", //$NON-NLS-1$
                "transformations/M2M", //$NON-NLS-1$
                "transformations/M2T", //$NON-NLS-1$
                "transformations/HOT", //$NON-NLS-1$
                "mtcs", //$NON-NLS-1$ 
                "metamodels", //$NON-NLS-1$
                "libraries", //$NON-NLS-1$
                "scripts", //$NON-NLS-1$
                "validations", //$NON-NLS-1$
                "tests", //$NON-NLS-1$
                "models", "META-INF" }; //$NON-NLS-1$
        addToProjectStructure(javaProject.getProject(), paths);
        createTemplateFileInProjectAt(javaProject.getProject(), "build.properties", "build.properties");
        createTemplateFileInProjectAt(javaProject.getProject(), "default.mtc", "/mtcs/default.mtc");
        createTemplateFileInProjectAt(javaProject.getProject(), "default_diagram.mtcd", "/mtcs/default.mtcd");
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
        manifest.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
        manifest.getMainAttributes().putValue("Bundle-Name", projectName);
        manifest.getMainAttributes().putValue("Bundle-SymbolicName", projectName);
        manifest.getMainAttributes().putValue("Bundle-Version", "1.0.0");
        manifest.getMainAttributes().putValue("Require-Bundle",
                "com.mtcflow.model,com.mtcflow.engine,com.mtcflow.engine.core");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        manifest.write(out);
        javaProject.getProject().getFile("/META-INF/MANIFEST.MF")
                .create(new ByteArrayInputStream(out.toByteArray()), true, null);
        return javaProject.getProject();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Error creating the project", ex);
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

private IJavaProject createProject(String projName) throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    if (projName == null || projName.trim().length() == 0)
        return null;

    // create eclipse project
    IProject project = root.getProject(projName);
    if (project.exists())
        project.delete(true, null);/*from   w ww  .ja  va 2 s .  c  o  m*/

    project.create(null);
    project.open(null);

    addNature(project);

    // create java project
    IJavaProject javaProject = JavaCore.create(project);

    // add bin/ouput folder
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    // add libs to project class path
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // create source folder
    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(srcRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    return javaProject;
}

From source file:com.nginious.http.plugin.NewProjectWizard.java

License:Apache License

public boolean performFinish() {
    String name = pageOne.getProjectName();
    IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(name);

    if (newProject.exists()) {
        throw new RuntimeException("Project exists!");
    }//w w  w.  j a  v a 2  s . co  m

    try {
        int listenPort = pageOne.getListenPort();

        if (!pageOne.validate(listenPort)) {
            return false;
        }

        IProgressMonitor progressMonitor = new NullProgressMonitor();

        // Create project
        newProject.create(progressMonitor);
        newProject.open(progressMonitor);

        // Create folder structure
        String[] paths = { "src", "WebContent", "WebContent/WEB-INF", "WebContent/WEB-INF/classes",
                "WebContent/WEB-INF/lib", "WebContent/WEB-INF/xsp" };
        addToProjectStructure(newProject, paths);

        ClassPathBuilder builder = new ClassPathBuilder(newProject, progressMonitor);
        builder.build(progressMonitor);

        // Set project nature
        IProjectDescription description = newProject.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID, NginiousPlugin.NATURE_ID });
        newProject.setDescription(description, null);

        // Create java project
        IJavaProject javaProject = JavaCore.create(newProject);

        // Set classes output folder
        IFolder classesFolder = newProject.getFolder("WebContent/WEB-INF/classes");
        javaProject.setOutputLocation(classesFolder.getFullPath(), null);

        // Set classpath
        IClasspathEntry[] entries = builder.getClassPath();
        javaProject.setRawClasspath(entries, progressMonitor);

        // Set source folder
        IFolder sourceFolder = newProject.getFolder("src");
        IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
        System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
        newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
        javaProject.setRawClasspath(newEntries, null);
        BasicNewProjectResourceWizard.updatePerspective(this.configurationElement);

        newProject.setPersistentProperty(NginiousPlugin.LISTEN_PORT_PROP_KEY, Integer.toString(listenPort));
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_URL_PROP_KEY, pageOne.getPublishUrl());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_USERNAME_PROP_KEY,
                pageOne.getPublishUsername());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_PASSWORD_PROP_KEY,
                pageOne.getPublishPassword());
        newProject.setPersistentProperty(NginiousPlugin.MIN_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMinMemory()));
        newProject.setPersistentProperty(NginiousPlugin.MAX_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMaxMemory()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return true;
}

From source file:com.nginious.http.plugin.ServerManager.java

License:Apache License

private boolean updateProjectWithPluginVersion(IProject project) {
    logger.log("ENTER ServerManager.updateProjectWithPluginVersion project={0}", project);

    try {/* w ww .jav  a2  s.  c om*/
        URL apiJar = NginiousPlugin.getApiJar();
        String filePath = apiJar.toString();
        filePath = filePath.substring(5);
        Path apiJarPath = new Path(filePath);

        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
        boolean changed = false;

        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IPath path = entry.getPath();

                if (path.lastSegment() != null && path.lastSegment().endsWith("nginious-api.jar")) {
                    changed = true;
                    entry = JavaCore.newLibraryEntry(apiJarPath, null, null);
                }
            }

            newEntries.add(entry);
        }

        if (changed) {
            IProgressMonitor progress = new NullProgressMonitor();
            entries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(entries, progress);
            javaProject.save(progress, true);
        }

        logger.log("EXIT ServerManager.updateProjectWithPluginVersion changed={0}", changed);
        return changed;
    } catch (JavaModelException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    } catch (IOException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    }
}