简体   繁体   English

JAVA:如何获取当前在Eclipse中打开的文件的名称

[英]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. 我为自动化框架开发了一个执行程序(jar文件),该执行程序连接到远程计算机并执行用eclipse编写的脚本,并将结果返回到本地计算机。 Right now I have to mention the name of file and it's path before I trigger the executor in eclipse using Ctrl+F11 现在,我必须使用Ctrl + F11在eclipse中触发执行程序之前,必须提及文件名及其路径。

Modification: 修改:

I want it to run the script which is open in eclipse when I run my executor using Ctrl+F11 , eg I open myscript.txt in eclipse and hit run and it should execute myscript.txt . 我希望它运行在我使用Ctrl + F11运行执行程序时在eclipse中打开的脚本,例如,我在eclipse中打开myscript.txt并点击run,它应该执行myscript.txt

For this I would need the name of file which is currently open in eclipse. 为此,我需要当前在eclipse中打开的文件名。

NOTE: I have not developed a plugin it is just a simple core Java code. 注意:我尚未开发插件,它只是一个简单的核心Java代码。

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 . 但是它总是让我回想起Work Bench does not exist

I am new to it, please let me know if something does not make sense here. 我是新手,如果这里没有意义,请告诉我。

-Thanks in advance -提前致谢

When you run an application via a run configuration Eclipse starts an own javaw process for that. 通过运行配置运行应用程序时,Eclipse为此启动了一个自己的javaw进程。 Therefore you cannot access the environment of Eclipse from inside that process in the way you tried. 因此,您无法以尝试的方式从该过程内部访问Eclipse环境。

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. 使您的应用程序期望打开的文件作为命令行参数传递到其main方法。
  2. Enter ${resource_loc} in the "program arguments" section of the run configuration. 在运行配置的“程序参数”部分中输入${resource_loc} ${resource_loc} translates into the absolute file system path of the selected resource. ${resource_loc}转换为所选资源的绝对文件系统路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM