PixelMaster
PixelMaster

Reputation: 954

Opening files in eclipse via code

I would like to open .java files in Eclipse programatically, i.e. using code to launch Eclipse (if it's not already open) and then opening the specified file.

Basically, something like this (non-working, examplary) code:

File file = new File("path/to/file.txt");
EclipseEditor.open(file);

Now, I've done a little research and for some time thought I could do this by using org.eclipse.stuff, as in this example. However, after some errors trying to even get the required packages/classes on my build path, I now just get an ExceptionInInitializerError when I call EFS.getLocalFileSystem();.

I'm wondering if these packages might be for eclipse plugins only, and if not, how I could use them as desired.

If they only work with plugins inside an already running eclipse, that's also fine - my application would also be viable as a plugin. I wanted to avoid reading up on how to write plugins, though - so if there is any way to avoid using plugins, I would greatly appreciate it.

Upvotes: 0

Views: 2675

Answers (1)

howlger
howlger

Reputation: 34325

You can trigger the command line eclipse --launcher.openFile "path/to/file.txt" to open one or more files in Eclipse, e. g. via the following Java code:

Runtime.getRuntime().exec(new String[] {
    "/path/to/eclipse",
    "--launcher.openFile",
    "path/to/file.txt",
    // "path/to/file2.txt",
    // ...
});

Alternatively, you can use Eclipse EASE from inside of Eclipse and open a file via e. g. JavaScript with openEditor(file) (using the /System/Resources and the /System/UI modules) like in this more complex example.

Upvotes: 3

Related Questions