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?
"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);
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With