9

I have a .classpath file of a project which contains all classpath entries. Now it has following entry-

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
</classpathentry>

Now, from this entry , I want to find all jars which are associated with this library through java code programatically ? Is there any way to read all jars?

3
  • Is it a requirement that this program could run stand-alone, outside of a plugin running in Eclipse? I have a little experience with Eclipse plugins and I have been at the source for Eclipse's classpath reader. Jan 1, 2017 at 22:34
  • yes.. it could run standalone also
    – Disha
    Jan 2, 2017 at 6:05
  • Just checking in. With no feedback on my answer, I don't know if you need me to dig deeper or be more explicit. Jan 6, 2017 at 23:26

2 Answers 2

3
+25

"con" is a container like JRE and it is related to parent classloader in your application. This conatiner has its own classpath defined and it could be read in runtime:

public static void main(String[] args) {
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    ClassLoader parentClassLoader = classLoader.getParent();
    System.out.println("Project jars: ");
    readJars(classLoader);
    System.out.println("Container jars: ");
    readJars(parentClassLoader);
}

private static void readJars(ClassLoader classLoader) {
    URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
    URL[] urls = urlClassLoader.getURLs();
    for(URL url: urls){
        String filePath = url.getFile();
        File file = new File(filePath);
        if(file.exists() && file.isFile()) {
            //Apply additional filtering if needed
            System.out.println(file);
        }
    }
}
2

The following was investigated running Eclipse JEE Kepler while reading source code that was checked out summer 2016 and debugging Eclipse on startup.

In your workspace root folder there is a file .metadata.plugins\org.eclipse.jdt.core\variablesAndContainers.dat. This file is read by JavaModelManager from the method loadVariablesAndContainers.

Here is the source of JavaModelManager https://git.eclipse.org/c/e4/org.eclipse.jdt.core.git/tree/model/org/eclipse/jdt/internal/core/JavaModelManager.java

Within variablesAndContainers.dat, I believe there is an entry for each project, and each project has a container. You can see the container name as a String in the file.

Flow continues to JavaModelManager$VariablesAndContainersLoadHelper.loadContainers(IJavaProject)

From here, the file reads a count of the number of classpath entries. For each entry, it then reads the container with the method VariablesAndContainersLoadHelper.loadClasspathEntry. This creates an array of classpath entries which represents the Java container. This is held in memory as JavaModelManager.PersistedClasspathContainer.

This is what you are looking for if creating a standalone application. If creating an Eclipse plugin, examine the behavior of JavaModelManager.getClasspathContainer.

You'll have to study the code, and maybe debug a lot of Eclipse startups to figure out the whole format of the file.

1
  • I am going through ur suggestions and yt not able to achieve the solution.. it is surely helpful . But i have not accepted it yet as i didn't get output.
    – Disha
    Jan 8, 2017 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.