Quinn Bailey
Quinn Bailey

Reputation: 200

Better way to extract resolved Eclipse build classpath from Eclipse projects

I'm looking into a means of extracting Eclipse build classpath info from Eclipse projects. While I'm pretty sure this is achievable by writing a custom headless Eclipse application that loads the Java projects and calls a method to resolve the classpaths (see this SO question), I'm trying to avoid that if possible. I'm not opposed to this if it's the best/only way, but before I do this I wanted to check if someone has already solved this problem.

All I need out of this is a list of jar files included in the project's classpath. Most of this info can be collected from the .classpath file itself. However, the difficulty I'm having is with the so-called "classpath containers" which are stored internal to eclipse in an apparently undocumented format. I've looked to some existing tools to solve this, in particular ant4eclipse, but last I checked the only classpath container types it can resolve are JREs and User Libraries, both of which I've already figured out how to tackle without needing to invoke eclipse.

Any suggestions?

Upvotes: 2

Views: 366

Answers (2)

nitind
nitind

Reputation: 20003

If you want a correct and accurate result, you want JDT to resolve them for you. So yes, you need to write a headless Eclipse Application.

Upvotes: 1

kapex
kapex

Reputation: 29959

I don't know if there is any eclipse specific way, but in a standard java application you can use the java.class.path system property to get classpath entries.

System.getProperty("java.class.path")

The paths are separated with a OS specific delimiter. The classpath will usually contain the compilers output directory (e.g. bin or target/classes) and any jars that have been added to the classpath. You can get all jars like this:

for (String classPathEntry : System.getProperty("java.class.path").split(File.pathSeparator)) {
    if (classPathEntry.endsWith(".jar")) {
        // ... 
    }
}

There is also a sun.boot.class.path system property for JRE libraries.

Upvotes: 0

Related Questions