Waman
Waman

Reputation: 1179

JAVA: How to get the name of file which is currently opened in eclipse

I have developed an executor (jar file) for my automation framework, this executor connects to a remote machine and executes my script written in eclipse and gets the results back to my local machine. Right now I have to mention the name of file and it's path before I trigger the executor in eclipse using Ctrl+F11

Modification:

I want it to run the script which is open in eclipse when I run my executor using Ctrl+F11, e.g. I open myscript.txt in eclipse and hit run and it should execute myscript.txt.

For this I would need the name of file which is currently open in eclipse.

NOTE: I have not developed a plugin it is just a simple core Java code.

I have tried the below code:

if(PlatformUI.isWorkbenchRunning()) {
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    String name = activePage.getActiveEditor().getEditorInput().getName();
    System.out.println(name);
}
else
    System.out.println("Work Bench Does not exist");

But it always returns me that Work Bench does not exist.

I am new to it, please let me know if something does not make sense here.

-Thanks in advance

Upvotes: 0

Views: 123

Answers (1)

wero
wero

Reputation: 32980

When you run an application via a run configuration Eclipse starts an own javaw process for that. Therefore you cannot access the environment of Eclipse from inside that process in the way you tried.

There might be a simpler solution to your problem:

  1. Make your application expect the opened file to be passed as commandline argument to its main method.
  2. Enter ${resource_loc} in the "program arguments" section of the run configuration. ${resource_loc} translates into the absolute file system path of the selected resource.

Upvotes: 1

Related Questions