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:net.sf.jasperreports.eclipse.builder.action.ToggleNatureAction.java

License:Open Source License

/**
 * Toggles sample nature on a project//w  w  w .  j  a  va  2 s.c o m
 * 
 * @param project
 *          to have sample nature added or removed
 */
private void toggleNature(IProject project, IProgressMonitor monitor) {
    try {
        IProjectDescription description = project.getDescription();
        String[] natures = description.getNatureIds();
        IJavaProject javaProject = JavaCore.create(project);
        for (int i = 0; i < natures.length; ++i) {
            if (JasperReportsNature.NATURE_ID.equals(natures[i])) {
                // Remove the nature
                String[] newNatures = new String[natures.length - 1];
                System.arraycopy(natures, 0, newNatures, 0, i);
                System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
                description.setNatureIds(newNatures);
                project.setDescription(description, monitor);

                // Path to all libraries needed

                List<IClasspathEntry> centries = new ArrayList<IClasspathEntry>();
                IClasspathEntry[] entries = javaProject.readRawClasspath();
                Set<Path> set = JasperReportsPlugin.getClasspathContainerManager().getRemovableContainers();
                for (IClasspathEntry en : entries) {
                    if (en.getPath().equals(JRClasspathContainer.ID))
                        continue;
                    if (set.contains(en.getPath()))
                        continue;
                    centries.add(en);
                }
                javaProject.setRawClasspath(centries.toArray(new IClasspathEntry[centries.size()]), monitor);
                return;
            }
        }

        // Add the nature
        ProjectUtil.addNature(project, JasperReportsNature.NATURE_ID, monitor);
        ProjectUtil.createJRClasspathContainer(monitor, javaProject);
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.jasperreports.eclipse.wizard.project.ProjectUtil.java

License:Open Source License

public static void createJRClasspathContainer(IProgressMonitor monitor, List<IClasspathEntry> centries,
        IJavaProject javaProject) throws JavaModelException {
    JRClasspathContainer classpathContainer = new JRClasspathContainer(null, javaProject);
    JavaCore.setClasspathContainer(JRClasspathContainer.ID, new IJavaProject[] { javaProject },
            new IClasspathContainer[] { classpathContainer }, monitor);
    centries.add(JavaCore.newContainerEntry(JRClasspathContainer.ID, true));
    javaProject.setRawClasspath(centries.toArray(new IClasspathEntry[centries.size()]), monitor);

    JasperReportsPlugin.getClasspathContainerManager().createJRClasspathContainer(monitor, centries,
            javaProject);/*from   w ww  .  ja  v  a 2 s  .c  o m*/
}

From source file:net.sf.jasperreports.eclipse.wizard.project.ProjectUtil.java

License:Open Source License

public static void addFileToClasspath(IProgressMonitor monitor, IFile file) throws CoreException {
    if (file.getProject().getNature(JavaCore.NATURE_ID) != null) {
        IJavaProject jprj = JavaCore.create(file.getProject());
        List<IClasspathEntry> centries = new ArrayList<IClasspathEntry>();
        IClasspathEntry[] entries = jprj.readRawClasspath();
        for (IClasspathEntry en : entries) {
            if (en.getPath().equals(file.getFullPath()))
                return;
        }//from  w  w  w  . j a  va  2  s  . com
        centries.add(JavaCore.newLibraryEntry(file.getFullPath(), null, new Path("/")));
        centries.addAll(Arrays.asList(entries));
        jprj.setRawClasspath(centries.toArray(new IClasspathEntry[centries.size()]), monitor);
    }
}

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

License:Open Source License

private static void addLibraries(Set<String> lpaths, IJavaProject project, IProgressMonitor monitor) {
    if (lpaths.isEmpty())
        return;//w w  w  .  j av a 2  s  . c o  m
    try {
        IClasspathEntry[] cpentries = project.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[cpentries.length + lpaths.size()];
        System.arraycopy(cpentries, 0, newEntries, 0, cpentries.length);
        int i = cpentries.length;
        for (String cp : lpaths) {
            newEntries[i] = JavaCore.newLibraryEntry(project.getProject().getFile(cp).getFullPath(), null,
                    null);
            i++;
            if (monitor.isCanceled())
                return;
        }
        project.setRawClasspath(newEntries, monitor);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

}

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

License:Open Source License

private static void addSourceFolders(Set<String> cpaths, IJavaProject project, IProgressMonitor monitor) {
    if (cpaths.isEmpty())
        return;/*from   w ww. j av  a 2 s .c  om*/
    try {
        IClasspathEntry[] cpentries = project.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[cpentries.length + cpaths.size()];
        System.arraycopy(cpentries, 0, newEntries, 0, cpentries.length);
        int i = cpentries.length;
        for (String cp : cpaths) {
            newEntries[i] = JavaCore.newSourceEntry(project.getProject().getFile(cp).getFullPath());
            i++;
            if (monitor.isCanceled())
                return;
        }
        project.setRawClasspath(newEntries, monitor);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

}

From source file:net.sf.rcer.jcoimport.ProjectGenerator.java

License:Open Source License

/**
 * Creates a plug-in project for the SAP JCo from the source file specified.
 * @param monitor// w w  w  .jav a 2 s .  c o m
 * @param sourceFileName
 * @param pluginName
 * @throws CoreException 
 * @throws IOException 
 */
private void createJCoPluginProject(IProgressMonitor monitor, String sourceFileName, String pluginName)
        throws CoreException, IOException {

    monitor.subTask(MessageFormat.format(Messages.ProjectGenerator_CreatePluginTaskDescription, pluginName));

    // read the source file                                                                              10
    final Map<String, byte[]> files = readArchiveFile(sourceFileName);
    monitor.worked(10);

    // remove the project if it exists                                                                    5
    IProject project = workspaceRoot.getProject(pluginName);
    if (project.exists()) {
        project.delete(true, true, new SubProgressMonitor(monitor, 5));
    } else {
        monitor.worked(5);
    }

    // create and open the project                                                                       10
    project.create(new SubProgressMonitor(monitor, 5));
    project.open(new SubProgressMonitor(monitor, 5));

    // update the project description                                                                     5
    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID, PLUGIN_NATURE_ID });
    project.setDescription(description, new SubProgressMonitor(monitor, 5));

    // set the basic Java project properties                                                              5
    IJavaProject javaProject = JavaCore.create(project);
    IFolder binDir = project.getFolder("bin"); //$NON-NLS-1$
    IPath binPath = binDir.getFullPath();
    javaProject.setOutputLocation(binPath, new SubProgressMonitor(monitor, 5));

    // create empty jni folder                                                                            5
    project.getFolder("jni").create(true, true, new SubProgressMonitor(monitor, 5)); //$NON-NLS-1$

    // copy sapjco3.jar                                                                                  10
    project.getFile("sapjco3.jar").create(new ByteArrayInputStream(files.get("sapjco3.jar")), //$NON-NLS-1$ //$NON-NLS-2$
            true, new SubProgressMonitor(monitor, 10));

    // create META-INF and MANIFEST.MF                                                                   10
    // TODO use the version information from the MANIFEST.MF file in the archive
    IFolder metaInfFolder = project.getFolder("META-INF"); //$NON-NLS-1$
    metaInfFolder.create(true, true, new SubProgressMonitor(monitor, 5));
    StringBuilder manifest = new StringBuilder();
    manifest.append("Manifest-Version: 1.0\n"); //$NON-NLS-1$
    manifest.append("Bundle-ManifestVersion: 2\n"); //$NON-NLS-1$
    manifest.append("Bundle-Name: SAP Java Connector v3\n"); //$NON-NLS-1$
    manifest.append(MessageFormat.format("Bundle-SymbolicName: {0}\n", pluginName)); //$NON-NLS-1$
    manifest.append("Bundle-Version: 7.11.0\n"); //$NON-NLS-1$
    manifest.append("Bundle-ClassPath: bin/,\n"); //$NON-NLS-1$
    manifest.append(" sapjco3.jar,\n"); //$NON-NLS-1$
    manifest.append(" jni/\n"); //$NON-NLS-1$
    manifest.append("Bundle-Vendor: SAP AG, Walldorf (packaged using RCER)\n"); //$NON-NLS-1$
    manifest.append("Bundle-RequiredExecutionEnvironment: J2SE-1.5\n"); //$NON-NLS-1$
    manifest.append("Export-Package: com.sap.conn.jco,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.jco.ext,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.jco.monitor,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.jco.rt,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.jco.server\n"); //$NON-NLS-1$
    manifest.append("Bundle-ActivationPolicy: lazy\n"); //$NON-NLS-1$
    writeTextFile(monitor, manifest, metaInfFolder.getFile("MANIFEST.MF")); //$NON-NLS-1$

    // set classpath                                                                                      5
    final IPath jcoPath = new Path(MessageFormat.format("/{0}/sapjco3.jar", pluginName)); //$NON-NLS-1$
    IClasspathEntry jcoEntry = JavaCore.newLibraryEntry(jcoPath, Path.EMPTY, Path.EMPTY, true);
    final IPath jniPath = new Path(MessageFormat.format("/{0}/jni", pluginName)); //$NON-NLS-1$
    IClasspathEntry jniEntry = JavaCore.newLibraryEntry(jniPath, Path.EMPTY, Path.EMPTY, true);
    javaProject.setRawClasspath(new IClasspathEntry[] { jcoEntry, jniEntry },
            new SubProgressMonitor(monitor, 5));

    // create build.properties                                                                            5
    StringBuilder buildProperties = new StringBuilder();
    buildProperties.append("bin.includes = META-INF/,\\\n"); //$NON-NLS-1$
    buildProperties.append("               sapjco3.jar,\\\n"); //$NON-NLS-1$
    buildProperties.append("               jni/,\\\n"); //$NON-NLS-1$
    buildProperties.append("               .\n"); //$NON-NLS-1$
    writeTextFile(monitor, buildProperties, project.getFile("build.properties")); //$NON-NLS-1$

    // collect the plug-in for export
    exportableBundles.add(modelManager.findModel(project));
}

From source file:net.sf.rcer.jcoimport.ProjectGenerator.java

License:Open Source License

/**
 * Creates a plug-in project for the SAP IDoc library from the source file specified.
 * @param monitor//from w  ww  . ja  v  a  2 s .co m
 * @param sourceFileName
 * @param pluginName
 * @throws CoreException 
 * @throws IOException 
 */
private void createIDocPluginProject(IProgressMonitor monitor, String sourceFileName, String pluginName,
        String pluginNameJCo) throws CoreException, IOException {

    monitor.subTask(MessageFormat.format(Messages.ProjectGenerator_CreatePluginTaskDescription, pluginName));

    // read the source file                                                                              10
    final Map<String, byte[]> files = readArchiveFile(sourceFileName);
    monitor.worked(10);

    // remove the project if it exists                                                                    5
    IProject project = workspaceRoot.getProject(pluginName);
    if (project.exists()) {
        project.delete(true, true, new SubProgressMonitor(monitor, 5));
    } else {
        monitor.worked(5);
    }

    // create and open the project                                                                       10
    project.create(new SubProgressMonitor(monitor, 5));
    project.open(new SubProgressMonitor(monitor, 5));

    // update the project description                                                                     5
    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID, PLUGIN_NATURE_ID });
    project.setDescription(description, new SubProgressMonitor(monitor, 5));

    // set the basic Java project properties                                                              5
    IJavaProject javaProject = JavaCore.create(project);
    IFolder binDir = project.getFolder("bin"); //$NON-NLS-1$
    IPath binPath = binDir.getFullPath();
    javaProject.setOutputLocation(binPath, new SubProgressMonitor(monitor, 5));

    // copy sapidoc3.jar                                                                                 15
    project.getFile("sapidoc3.jar").create(new ByteArrayInputStream(files.get("sapidoc3.jar")), //$NON-NLS-1$ //$NON-NLS-2$
            true, new SubProgressMonitor(monitor, 15));

    // create META-INF and MANIFEST.MF                                                                   10
    // TODO use the version information from the MANIFEST.MF file in the archive
    IFolder metaInfFolder = project.getFolder("META-INF"); //$NON-NLS-1$
    metaInfFolder.create(true, true, new SubProgressMonitor(monitor, 5));
    StringBuilder manifest = new StringBuilder();
    manifest.append("Manifest-Version: 1.0\n"); //$NON-NLS-1$
    manifest.append("Bundle-ManifestVersion: 2\n"); //$NON-NLS-1$
    manifest.append("Bundle-Name: SAP IDoc Library v3\n"); //$NON-NLS-1$
    manifest.append(MessageFormat.format("Bundle-SymbolicName: {0}\n", pluginName)); //$NON-NLS-1$
    manifest.append("Bundle-Version: 7.11.0\n"); //$NON-NLS-1$
    manifest.append("Bundle-ClassPath: bin/,\n"); //$NON-NLS-1$
    manifest.append(" sapidoc3.jar\n"); //$NON-NLS-1$
    manifest.append("Bundle-Vendor: SAP AG, Walldorf (packaged using RCER)\n"); //$NON-NLS-1$
    manifest.append("Bundle-RequiredExecutionEnvironment: J2SE-1.5\n"); //$NON-NLS-1$
    manifest.append("Export-Package: com.sap.conn.idoc,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.jco,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.cp,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.record,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.record.impl,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.trace,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.util,\n"); //$NON-NLS-1$
    manifest.append(" com.sap.conn.idoc.rt.xml\n"); //$NON-NLS-1$
    manifest.append("Bundle-ActivationPolicy: lazy\n"); //$NON-NLS-1$
    manifest.append(MessageFormat.format("Require-Bundle: {0}\n", pluginNameJCo)); //$NON-NLS-1$
    writeTextFile(monitor, manifest, metaInfFolder.getFile("MANIFEST.MF")); //$NON-NLS-1$

    // set classpath                                                                                      5
    final IPath jcoPath = new Path(MessageFormat.format("/{0}/sapidoc3.jar", pluginName)); //$NON-NLS-1$
    IClasspathEntry jcoEntry = JavaCore.newLibraryEntry(jcoPath, Path.EMPTY, Path.EMPTY, true);
    javaProject.setRawClasspath(new IClasspathEntry[] { jcoEntry }, new SubProgressMonitor(monitor, 5));

    // create build.properties                                                                            5
    StringBuilder buildProperties = new StringBuilder();
    buildProperties.append("bin.includes = META-INF/,\\\n"); //$NON-NLS-1$
    buildProperties.append("               sapidoc3.jar,\\\n"); //$NON-NLS-1$
    buildProperties.append("               .\n"); //$NON-NLS-1$
    writeTextFile(monitor, buildProperties, project.getFile("build.properties")); //$NON-NLS-1$

    // collect the plug-in for export
    exportableBundles.add(modelManager.findModel(project));
}

From source file:net.sourceforge.floggy.eclipse.ToggleNatureAction.java

License:Open Source License

private void updateClasspath(IProject project) throws Exception {
    IJavaProject javaProject = JavaCore.create(project);
    List rawClasspath = new LinkedList(Arrays.asList(javaProject.getRawClasspath()));

    boolean contains = false;

    for (int i = 0; i < rawClasspath.size(); i++) {
        IClasspathEntry classpath = (IClasspathEntry) rawClasspath.get(i);

        if (classpath.getPath().toOSString().indexOf("floggy-persistence-framework") != -1) {
            contains = true;//from  ww  w .  j  a  va  2 s . com

            break;
        }
    }

    if (!contains) {
        Enumeration e = Activator.findEntries("/", "*.jar", true);

        while (e.hasMoreElements()) {
            URL url = (URL) e.nextElement();
            String path = FileLocator.toFileURL(url).getPath();

            if (path.indexOf("floggy-persistence-framework") != -1) {
                String javadocURL = "http://floggy.sourceforge.net/modules/floggy-persistence-framework/apidocs/";

                String version = getVersion(path);

                if (version != null) {
                    javadocURL = "http://floggy.sourceforge.net/modules/floggy-persistence-framework/" + version
                            + "/floggy-persistence-framework/apidocs/";
                }

                IClasspathAttribute attribute = JavaCore.newClasspathAttribute("javadoc_location", javadocURL);
                IClasspathEntry varEntry = JavaCore.newLibraryEntry(new Path(path), null, null, null,
                        new IClasspathAttribute[] { attribute }, true);
                rawClasspath.add(varEntry);
                javaProject.setRawClasspath(
                        (IClasspathEntry[]) rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]),
                        null);

                break;
            }
        }

        if (Activator.isMTJAvailble()) {
            IFile implJar = project.getFile("floggy-persistence-framework-impl.jar");

            if (!implJar.exists()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                JarOutputStream out = new JarOutputStream(baos, new Manifest());

                out.close();

                implJar.create(new ByteArrayInputStream(baos.toByteArray()), IResource.DERIVED, null);
            }

            IPath filePatternImpl = new Path("net/sourceforge/floggy/persistence/impl/*");
            IPath filePatternImplMigration = new Path("net/sourceforge/floggy/persistence/impl/migration/*");

            IAccessRule[] accessRules = new IAccessRule[2];

            accessRules[0] = JavaCore.newAccessRule(filePatternImpl, IAccessRule.K_NON_ACCESSIBLE);
            accessRules[1] = JavaCore.newAccessRule(filePatternImplMigration, IAccessRule.K_NON_ACCESSIBLE);

            IClasspathEntry entry = JavaCore.newLibraryEntry(implJar.getLocation(), null, null, accessRules,
                    null, true);

            rawClasspath.add(entry);
            javaProject.setRawClasspath(
                    (IClasspathEntry[]) rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null);
        }
    }
}

From source file:net.ssehub.easy.producer.ui.project_management.EASyJavaConfigurator.java

License:Apache License

@Override
public void configure(IProject project, IProject parentProject) {
    JavaCapabilityConfigurationPage jcpage = new JavaCapabilityConfigurationPage();
    IJavaProject javaProject = JavaCore.create(project);
    IJavaProject javaParentProject = JavaCore.create(parentProject);

    jcpage.init(javaProject, null, null, false);
    try {/*from w  ww .j av a  2 s  . c  o m*/
        jcpage.configureJavaProject(null);
    } catch (CoreException e1) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e1);
    } catch (InterruptedException e1) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e1);
    }

    // Try to copy settings from parent
    try {
        IClasspathEntry[] parrentEntries = javaParentProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[parrentEntries.length];

        // Copy Classpath Entries
        for (int i = 0; i < newEntries.length; i++) {
            IClasspathEntry parentEntry = parrentEntries[i];
            newEntries[i] = javaProject
                    .decodeClasspathEntry(javaParentProject.encodeClasspathEntry(parentEntry));

            try {
                IPath entryPath = newEntries[i].getPath().makeAbsolute();
                if (project.getFullPath().matchingFirstSegments(entryPath) > 0) {
                    File entryFile = entryPath.toFile();
                    IFolder folder = project.getFolder(entryFile.getName());
                    if (!folder.exists()) {
                        folder.create(false, true, null);
                    }
                }
            } catch (CoreException e) {
                EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID)
                        .exception(e);
            }
        }
        javaProject.setRawClasspath(newEntries, null);
    } catch (CoreException e) {
        EASyLoggerFactory.INSTANCE.getLogger(EASyJavaConfigurator.class, Activator.PLUGIN_ID).exception(e);
    }

}

From source file:nz.ac.auckland.ptjava.builder.PTJavaClasspath.java

License:Open Source License

/**
 * Add the PTRuntime.jar to the classpath of javaProject
 * /*from  ww  w .  ja v a2s.  c  o m*/
 * @param javaProject
 * @param monitor
 */
public static void addPTRuntimeToClasspath(IJavaProject javaProject, IProgressMonitor monitor) {
    try {
        IClasspathEntry entries[] = javaProject.getRawClasspath();
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>(entries.length + 1);
        for (int i = 0; i < entries.length; i++) {
            list.add(entries[i]);
        }

        // add the java runtime library
        IPath path = null;
        IPreferenceStore prefs = PTJavaPlugin.getDefault().getPreferenceStore();
        if (prefs.getBoolean(PreferenceConstants.PTJAVA_USE_CUSTOM_RUNTIME)) {
            path = new Path(prefs.getString(PreferenceConstants.PTJAVA_RUNTIME_PATH));
        } else {
            path = PTJavaPreferencePage.getDefaultRuntimeJarPath();
        }
        list.add(JavaCore.newLibraryEntry(path, null, null));

        IClasspathEntry updatedEntries[] = new IClasspathEntry[list.size()];
        list.toArray(updatedEntries);

        javaProject.setRawClasspath(updatedEntries, monitor);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}