views:

98

answers:

1

I am stuck in a great problem, I need to change the order of two jar files in java build path of eclipse. there is some operation on which the junit3.jar will be set before junit4.jar, but i am not able to find any clue.

Please help me... Thanks in advance.

A: 

You can obtain the raw classpath for the project, then find each Junit entry based on its kind and path, if they're in the "wrong order", you can then modify the raw classpath and set the modified path on to the project.

The snippet below outlines how it can be done, note there's no exception handling in this example:

//get the project by name, you probably want to use another method to 
//obtain it
IProject project = ResourcesPlugin.getWorkspace().getRoot()
        .getProject("foo");
IJavaProject javaProject = JavaCore.create(project);

IClasspathEntry[] entries = javaProject.getRawClasspath();

// find the JUnit 3 and Junit 4 entry index
int junit3Index = -1;
int junit4Index = -1;
for (int i = 0; i < entries.length; i++) {
    if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        if (entries[i].getPath().equals(
                JUnitContainerInitializer.JUNIT3_PATH)) {
            junit3Index = i;
        } else if (entries[i].getPath().equals(
                JUnitContainerInitializer.JUNIT4_PATH)) {
            junit4Index = i;
        }
    }
}

if (junit3Index != -1 && junit4Index != -1
        && junit3Index > junit4Index) {
    // swap the two entries
    IClasspathEntry temp = entries[junit4Index];
    entries[junit4Index] = entries[junit3Index];
    entries[junit3Index] = temp;

    //update the project with the modified path
    javaProject.setRawClasspath(entries, new NullProgressMonitor());
}
Rich Seller