设置eclipse插件的工作目录 [英] Setting the working directory for an eclipse plug-in

查看:223
本文介绍了设置eclipse插件的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开发的Eclipse插件中,我正在查询我的开发目录中的一些Prolog文件。另外,位于我工作目录根目录的初始化文件由我使用的Prolog引擎自动加载。所有这些文件都是我的插件的资源。



当我以java独立方式执行我的应用程序时,我唯一需要做的就是在Eclipse Run Configurations对话框中设置,工作目录到我的默认输出目录。然后当执行我的应用程序时,Prolog引擎从初始化文件所在的目录开始,并且可以在需要时找到并加载应用程序的prolog文件。这个Prolog引擎使用JPL库从Java启动。



当我想要构建我的应用程序的Eclipse插件时,我的问题开始了。显然,在运行配置对话框中不能更改插件的默认工作目录(至少在我的Mac设置中为这种情况),否则在执行插件时会出现无限列表。
所以我想知道的是,如果在开发生命周期(即当我将Eclipse插件作为Eclipse应用程序启动时)和生产(即,当插件在插件目录中时)。



谢谢!!

解决方案

为应用程序设置工作目录而不是插件。我可以想象如果所有插件都会尝试将工作目录设置为自己的喜好,那么这个混乱...



插件中的文件可以使用 Bundle 类可以使用插件激活器访问。这将给你一个最可能指向JAR条目的URL。 FileLocator 类可用于将该URL转换为文件URL(文件将从JAR中提取)。

  URL url = Activator.getDefault()。getBundle()。getEntry(JAR中的文件路径); 
URL fileUrl = FileLocator.toFileURL(url);
文件文件= new File(url.getPath());

如果文件必须在工作目录中,那么可以将其复制到


In the Eclipse plugin I am developing, I am querying some Prolog files that are in my development directory. In addition, an initialization file located at the root of my working directory is automatically loaded by the Prolog engine I am using. All these files are resources of my plugin.

When executing my application as a java standalone the only thing I need to do is to set, in the Eclipse Run Configurations dialog, the working directory to my default output directory. Then when executing my application the Prolog engine starts at the directory where the initialization file is located, and it is able to locate and load the prolog files of the application when needed. This Prolog engine is started from Java using the JPL library.

My problem started when I wanted to build an Eclipse plug-in of my application. Apparently the default working directory for plugins cannot be changed in the Run Configurations dialog (at least that is the case in my Mac setting), otherwise an infinite list of exceptions appear when the plug-in is executed. So what I would like to know is if there is another way to setup the working directory for a plugin, both during the development lifecycle (i.e., when I launch my plugin as an Eclipse Application) and in production (i.e., when the plugin is in the plugin directory).

Thanks !!

解决方案

Working directory is set for the application not a plugin. I can imagine the mess if all plugins would try to set the working directory to their liking...

Files in a plugin can be found using the Bundle class which can be accessed using plugin activator. This will give you a URL that most likely points to a JAR entry. FileLocator class can be used to "convert" that URL to a file URL (file will be extracted from JAR).

URL url = Activator.getDefault().getBundle().getEntry("file path in JAR");
URL fileUrl = FileLocator.toFileURL(url);
File file = new File(url.getPath());

If the file MUST be in the working directory then you can copy it there.

这篇关于设置eclipse插件的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆