Let's say I have a java file C:\workspace\project\src\packagename\foobar.java
. Now, I would like to compile this file at runtime, but for that I need the corresponding .classpath
file (or rather, it's entries), located at C:\workspace\project\.classpath
, so that I can pass the dependencies with the -classpath
argument (this approach works if I hard-code it, but that's obviously only proof-of-concept, not the goal).
How would I go about finding the .classpath
file?
My idea was walking the file tree bottom up, starting at foobar.java
, until I get to a level where a .classpath
file exists which I can then use.
However, I'm a) not sure that this is the way to do it, and b) not sure how to walk the file tree bottom up, other than String.split
-ting the file and removing the segments, starting at the end. That is super ugly, though, and I refuse to believe that there is no better option.
It sounds like you are asking how to write Java code that understands the layout and structure of say eclipse project directories. To then "mimic" eclipse and compile files for you.
This is probably a really bad idea. You see, unless you are talking about very small and "reduced" projects only, sooner or later, you will encounter projects that are large, and each one a bit different than the other. You will be constantly trying to get things right, and each time you fixed a bug, another project comes along and something new shows up and your solution breaks. Because you can't get a robust "copy" of the eclipse build code unless you use that build code.
Thus my suggestion: