如何引用eclipse插件中jar文件的文件 [英] How to refer a file from jar file in eclipse plugin

查看:224
本文介绍了如何引用eclipse插件中jar文件的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个eclipse插件,我想在eclipse运行时部署。我有以下的包装结构。

  com.myplugin 
|
---资源
|
--- server.bat

作为插件作业的一部分,server.bat 文件应该执行。



我将插件打包成.jar文件,包括二进制文件中的资源文件夹,并放入eclipseplugins文件夹。



插件生效,它工作正常,但执行server.bat文件时,我有一个问题,这是在我生成的jar内。错误消息说:


Windows找不到resources\server.bat确保您正确键入了
的名称,尝试再次


我尝试了相对路径和绝对路径,但没有工作。



这是正常工作的代码:

  URL url = Activator.getDefault()。getBundle()。 getEntry( /资源/ server.bat); 

String fileURL = FileLocator.toFileURL(url).toString();

String commandLine =cmd.exe / c start+ fileURL;

进程进程= Runtime.getRuntime()。exec(commandLine);

我得到了fileURL输出:



< blockquote>

file:/ D:/ Program
文件/ IBM / SDP / configuration / org.eclipse.osgi / bundles / 2392/1 / .cp / resources / server.bat


我不知道这是否正确。



希望这是清楚的回答问题。



或者,请建议其他方法,例如创建功能部署带有文件夹结构的插件。我还没有尝试过这个选项。

解决方案

当我导出我的插件时,我遇到了类似的问题。我不得不引用存储在我的插件jar文件中的一个exe文件。当插件被导出时,我无法访问压缩文件,当我开发插件时可以访问,因为eclipse在我的开发文件夹中查找文件。
为了解决这个问题,我创建了一个插件功能,在feature.xml的Included Plug-ins选项卡中,我查看了选项


安装后解压缩插件归档文件。


包含exe文件的插件。
使用这个选项你会发现你的插件文件在eclipse插件文件夹下的一个文件夹中,你将能够作为普通文件访问它们。



例如

  Bundle bundle =<获取一个插件的一部分> ;; 
URL url = FileLocator.find(bundle,new Path(<从插件根到你的文件的相对路径>),null);
try {
url = FileLocator.resolve(url);
} catch(IOException e){
e.printStackTrace();
返回false;
}

否则我认为你应该解压缩你的jar。



希望这个帮助。



编辑



正如我所说,您可以创建一个功能项目(例如, here )当您添加插件时,请选中安装后解压缩插件归档选项。 Eclipse将为您做的工作,您会发现您的插件在eclipse插件文件夹中解压缩。这解决了我的问题。


I have created an eclipse plugin and I wanted to deploy during eclipse runtime. I have below package structure.

com.myplugin
  |
   ---resources
       |
        ---server.bat

As part of the plugin job, "server.bat" file should be executed.

I packaged the plugin as .jar file including resouces folder in the binary and placed in to the eclipse "plugins" folder.

Plugin took effect and it does work fine, but I have a problem while executing the "server.bat" file, which is inside the jar that I generated. The error message says:

"Windows cannot find "resources\server.bat" make sure you typed name correctly and try again"

I tried with relative paths and absolute paths, but it didnt work.

Here is the code doing that work:

URL url = Activator.getDefault().getBundle().getEntry("/resources/server.bat");

String fileURL = FileLocator.toFileURL(url).toString();

String commandLine = "cmd.exe /c start " +fileURL;

Process process= Runtime.getRuntime().exec(commandLine);

I got the "fileURL" output:

file:/D:/Program Files/IBM/SDP/configuration/org.eclipse.osgi/bundles/2392/1/.cp/resources/server.bat

I am not sure this is correct.

Hope this is clear enough to answer the question.

Alternatively, please suggest some other way, such as creating features to deploy the plugin with folder structure. I haven't tried this option yet.

解决方案

I have had a similar problem when I export my plugin. I had to refer an exe file stored in my plugin jar file. When the plugin was exported I can not access the zipped file, while it is accessible when I develop the plugin cause eclipse looks for the file in my "development folder". To solve the problem I created a plugin feature and in the "Included Plug-ins" tab of feature.xml I checked the option

Unpack the plug-in archive after the installation.

for the plugin which contains the exe file. Using this option you will find your plugin files in a folder under the eclipse plugin folder and you will be able to access them as regular files.

For example

    Bundle bundle = <get a bundle of your plugin>;
    URL url = FileLocator.find(bundle, new Path(<relative path from plugin root to your file>), null);
    try {
        url = FileLocator.resolve(url);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

Otherwise I think you should decompress your jar.

Hope this help.

EDIT

As I said you can create a feature project(for an example look here) and when you add your plugins, check the option "Unpack the plug-in archive after the installation." Eclipse will do the work for you and you will find your plugin unzipped in the eclipse plugin folder. This solved the problem for me.

这篇关于如何引用eclipse插件中jar文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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