【问题标题】:how to load jar:file: uri in swt browser?如何在 swt 浏览器中加载 jar:file:uri?
【发布时间】:2014-01-09 17:57:53
【问题描述】:

我无法在 swt 浏览器中加载 jar:file uri。相同的 uri 在我的 Firefox 中可以正常加载,并且可以加载相同软件的旧版本。我能发现的唯一变化是旧的工作版本 - 似乎没有理由 - 依赖于org.eclipse.core.resources。 但是重新创建依赖后,页面仍然没有加载。

我在浏览器窗口中有这个:

Unable to load page

Problem occurred while loading the URL file:///usr/share/zenta/plugins/org.rulez.magwas.zenta.help_2.4.0.201401091630.jar!/hints/viewpoint_total.html

Error opening file: No such file or directory

我没有发现任何异常的迹象,日志中也没有任何痕迹。

上述案例中实际的uri是:

jar:file:/usr/share/zenta/plugins/org.rulez.magwas.zenta.help_2.4.0.201401091630.jar!/hints/viewpoint_total.html

我正在使用此代码:

    win.shell.pack();
    win.shell.open();
    Browser fBrowser = new Browser(win.getComposite(), SWT.NONE);
    GridData gd = new GridData(GridData.FILL_BOTH);
    fBrowser.setLayoutData(gd);
    fBrowser.pack();

    String cwd = System.getProperty("user.dir");
    String url = "jar:file:/usr/share/zenta/plugins/org.rulez.magwas.zenta.help_2.4.0.201401091630.jar!/hints/viewpoint_total.html"
    System.out.printf("cwd=%s\nurl=%s\n", cwd, url);
    fBrowser.setUrl(url);
    fBrowser.setFocus();

作为一个额外的问题:有没有办法检测浏览器的此类故障,所以当找到解决方案时,我可以为它编写一个单元测试?

【问题讨论】:

  • 其实问题不是版本不同,而是版本不同。似乎 swt 浏览器永远无法打开这样的 url(而 mozilla 是)。有问题的网址来自我的一个捆绑包。在 eclipse 构建中,有问题的包被打包为一个目录,而在 maven 构建中,它被打包为 jar,因为 tycho 忽略了 root.folder 属性。但是,我可以将 Eclipse-BundleShape: dir 添加到 MANIFEST.MF,由 tycho 执行。不过还是谢谢你的回答。

标签: java eclipse jar swt


【解决方案1】:

您可以使用JarUrlConnection获取资源:

  try {
    InputStream in;
    String url = "jar:file:/usr/share/zenta/plugins/org.rulez.magwas.zenta.help_2.4.0.201401091630.jar!/hints/viewpoint_total.html"
    JarURLConnection con = (JarURLConnection)url.openConnection();
    in = con.getInputStream();
    // read the stream
  } catch (MalformedURLException ex) {
    System.err.println("Malformed URL: "+url);
  } catch (IOException ex) {
    System.err.println("IO error");
  } finally {
    in.close();
  }

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 2012-06-18
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多