How to refer a file from jar file in eclipse plugi

2020-07-23 23:16发布

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.

5条回答
贪生不怕死
2楼-- · 2020-07-23 23:45

I found this worked well with Eclipse RCP 4, This code took the given gif image from a jar file that was in the given plugin

public ImageDescriptor getImgDesc(final String bundleId, final String fullPath)
 throws IOException {

  final URL url = new URL("platform:/plugin/" + bundleId + "/" + fullPath);

  final ImageDescriptor imgDesc = ImageDescriptor.createFromURL(url);
  return imgDesc;
}

....

final ImageDescriptor imgDesc  =  getImgDesc("com.awe.test","images/Applet24.gif");
final Image applet24 = imgDesc.createImage();
查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-23 23:55

I made it worked using ProcessBuilder. Below is the code snippet..

URL url = Platform.getBundle(Activator.PLUGIN_ID).getEntry("resources/server.bat");
String fileURL = FileLocator.toFileURL(url).toString();
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c",fileURL); 
pb.redirectErrorStream(true);
Process p = pb.start();
p.destroy();

Ofcourse, i extracted the jar and put that directory to plugins folder of eclipse.

查看更多
Explosion°爆炸
4楼-- · 2020-07-23 23:59

Use the second solution of http://www.vogella.com/blog/2010/07/06/reading-resources-from-plugin/

And don't forget to export the files you want to access in the build configuration of your plugin.

查看更多
手持菜刀,她持情操
5楼-- · 2020-07-24 00:02

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.

查看更多
爷、活的狠高调
6楼-- · 2020-07-24 00:11

Your code seems ok. Note that:

  1. You need to convert your fileURL to file path (i.e. remove "file:/" prefix). This is hugely platform-dependent - try to rely on org.eclipse.core.runtime.IPath and java.io.Path (I don't remember proper code, sorry)
  2. You need to escape the path as it contains space character.
查看更多
登录 后发表回答