2

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?

Community
  • 1
  • 1
Quinn Bailey
  • 200
  • 7
  • possible duplicate of [Access project build path through an eclipse plugin](http://stackoverflow.com/questions/12519297/access-project-build-path-through-an-eclipse-plugin) – Chris Gerken Oct 15 '14 at 01:24
  • @ChrisGerken I agree in part because that may be the only way to do it. The more I look at this, the more convinced I am that nitind is correct. However, invoking eclipse is a rather resource intensive operation to simply extract a list of jar files. I'm pretty sure there used to be a text metadata file that contained the resolved containers, but I believe the storage for that was changed to a serialized format back around version 3.4. – Quinn Bailey Oct 15 '14 at 05:36
  • Hmm ... it wouldn't let me edit my comment even though it definitely wasn't 5 minutes. Anyway, I would like to emphasize that the purpose of this question is to insure there isn't some shortcut I'm missing to get this info w/o invoking eclipse. Thanks – Quinn Bailey Oct 15 '14 at 05:44
  • 1
    You're correct. The problem is when you get into something like maven m2e which goes off into a different place entirely to resolve its class path entry. – Chris Gerken Oct 15 '14 at 10:00

2 Answers2

1

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.

nitind
  • 19,089
  • 4
  • 34
  • 43
0

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.

kapex
  • 28,903
  • 6
  • 107
  • 121
  • I believe this only gives you the classpath of the currently running JVM, but I need the classpath that Eclipse uses as it's build classpath, before the application is compiled and run. – Quinn Bailey Oct 14 '14 at 22:37