Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Only add JREContainerEntry when not yet set(Programmatically update JREContainerEntry when JavaProject doesnt contain one)
Only add JREContainerEntry when not yet set [message #1756440] Thu, 16 March 2017 21:36 Go to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
Hello everyone.

I am working on a importer for Bndtools and facing the problem that I dont want to add a JRE-Container if the Java-Project which has existing settings already has one.

List<IClasspathEntry> entries = new ArrayList<>(Arrays.asList(javaProject.getRawClasspath()));
entries.add(JavaRuntime.getDefaultJREContainerEntry());
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);


Thats what I am doing now, but this way, I could easily end up with two Container within the project. For example the java-project which I create already defined a JRE-7 container, but in my environment JRE-8 is the default. Unfortunately, the ClasspathEntries provide no real information which I could use to check if this entry is a JRE-container.

So, how can I check if a Java project has a JRE-container?
Thanks

[Updated on: Thu, 16 March 2017 21:38]

Report message to a moderator

Re: Only add JREContainerEntry when not yet set [message #1756447 is a reply to message #1756440] Fri, 17 March 2017 04:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 28973
Registered: July 2009
Senior Member
It seems to me that you can use org.eclipse.jdt.core.IClasspathEntry.getEntryKind() and test for org.eclipse.jdt.core.IClasspathEntry.CPE_CONTAINER. And in that case, if it's a JRE container, the path will conform to what's described in org.eclipse.jdt.launching.JavaRuntime.JRE_CONTAINER. Perhaps there's a better way...

Re: Only add JREContainerEntry when not yet set [message #1756500 is a reply to message #1756447] Fri, 17 March 2017 20:49 Go to previous message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
Thanks for the idea Ed.

edited:

The previous message was wrong because my workspace was already corrupted so that Classpath-entries were missing.

This is what I ended up with and seems to work for now

        boolean jreContainerAvailable = false;
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath() != null && entry.getPath().toString().startsWith(JavaRuntime.JRE_CONTAINER)) {
                jreContainerAvailable = true;
                break;
            }
        }
        if (!jreContainerAvailable) {
            entries.add(JavaRuntime.getDefaultJREContainerEntry());
            javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
        }

[Updated on: Tue, 21 March 2017 12:10]

Report message to a moderator

Previous Topic:Measuring compilation time of a Java project
Next Topic:Importing Cordova 6.1.2-Project to Eclipse: cordovalib.jar missing in Android Dependencies
Goto Forum:
  


Current Time: Thu Nov 23 03:50:14 GMT 2017

Powered by FUDForum. Page generated in 0.01754 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software