2016-09-06 130 views
0

我试图打开表示我的插件的文件从一个封装内的PDF文件,因为我已经开了一个属性文件使用getClass().getResource(URI)以同样的方式里面的PDF文件。 我想与PDF文件相同,我试图让文件的URL,然后将它传递给Desktop.browse()转换为URI,但它给了我一个Malformed URI例外。有没有办法做到这一点更容易,也有工作?Eclipse插件打开从插件包

这是到目前为止我的代码:

try{ 
     URL url = new URL(getClass().getResource("Documentation.pdf"), null); 
     Desktop.getDesktop().browse(url.toURI()); 
    }catch(Exception exception){ 
     Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, exception.getLocalizedMessage(), exception); 
     ErrorDialog.openError(null, "Error", "Error occured!", status); 
    } 

回答

1

的URL/URI从getResource回来使用不是很多东西理解bundleresource方案。

对于一个Eclipse插件,你应该使用FileLocator类。

Bundle bundle = FrameworkUtil.getBundle(getClass()); 

IPath path = new Path("path relative to root of the plugin"); 

URL url = FileLocator.find(bundle, path, null); 

URL fileURL = FileLocator.toFileURL(url); 

Desktop.getDesktop().browse(fileURL.toURI()); 

再次FileLocator.find返回的URL采用了特殊的方案,并没有被很多事情的理解。 FileLocator.toFileURL将此URL转换为正常的file方案 - 为此,可能需要将文件从插件jar解包到临时位置。

注:Pathorg.eclipse.core.runtime.Path

+0

什么是参数类型我应该使用'Path',似乎它不接受任何字符串参数。 – Justplayit94

+0

这是'org.eclipse.core.runtime.Path' **不是**'java.nio.file.Path' –

+0

就是这样。另外'Desktop.browse()'有错误'不能使静态参考从类型Desktop' – Justplayit94