【问题标题】:Launch webstart without downloading...?在不下载的情况下启动 webstart...?
【发布时间】:2012-07-25 15:13:30
【问题描述】:

我制作了一个 Java webstart 应用程序,并创建了一个带有启动它的链接的 HTML 页面。问题是,在 Google Chrome 中,没有选项可以“打开”文件而不保存它。我想制作一个无需保存即可自动启动 JNLP 文件的 HTML 页面。或者更确切地说,用户无需打开文件资源管理器即可启动它)这可能吗?

【问题讨论】:

标签: java google-chrome jnlp java-web-start


【解决方案1】:

对于测试或网页抓取(当您无法更改或控制 jnlp 处理时),我通过 Selenium + Python 找到了一种解决方法(但类似的事情在 Java 或其他语言中也应该是可行的)。在 Python 中,我只需以编程方式单击 Chrome 中的通知以允许下载和安装 jnlp 文件(在 win32api 和 win32con 的帮助下,但在代码返工后,类似的方法也可以在 Linux 或 Mac 上工作)。可以查看详情here

【讨论】:

    【解决方案2】:

    这个示例(在 Swing 中嵌入 JavaFX 2)和文章是一个很好的示例,它们也适用于现代浏览器

    样本http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html

    文档:https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html#BABIJEHC

    【讨论】:

      【解决方案3】:

      在厌倦了这个问题之后,我围绕扩展编写了自己的工作。

      它是在 ubuntu 下编写的,但应该是可移植的(即使是在 win32 上进行一些工作/阅读)。

      单击启动 jnlp 文件,无需提示或下载。它只是将 jnlp 文件的 url 直接传递给 javaws。没有杂乱的下载文件夹,没有额外的点击。

      简单、粗暴、有效。我过滤了 URL,所以它只适用于我自己的内部服务器,所以我不会意外启动一些随机的 jnlp 文件。我敢肯定,可以做更多的工作来改进它。按原样使用,无保修等。

      文件:

      /usr/local/bin/jnlp-launcher

      #!/usr/bin/env python
      
      import struct
      import sys
      import threading
      import Queue
      import json
      import os
      
      
      # On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
      # to avoid unwanted modifications of the input/output streams.
      if sys.platform == "win32":
        import os, msvcrt
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
      
      # Helper function that sends a message to the webapp.
      def send_message(message):
         # Write message size.
        sys.stdout.write(struct.pack('I', len(message)))
        # Write the message itself.
        sys.stdout.write(message)
        sys.stdout.flush()
      
      # Thread that reads messages from the webapp.
      def read_thread_func(queue):
        message_number = 0
        while 1:
          # Read the message length (first 4 bytes).
          text_length_bytes = sys.stdin.read(4)
      
          if len(text_length_bytes) == 0:
            if queue:
              queue.put(None)
            sys.exit(0)
      
          # Unpack message length as 4 byte integer.
          text_length = struct.unpack('i', text_length_bytes)[0]
      
          # Read the text (JSON object) of the message.
          text = sys.stdin.read(text_length).decode('utf-8')
      
          decoded = json.loads(text);
          os.system("javaws " + decoded['url']);
      
      
      def Main():
        read_thread_func(None)
        send_message('"complete"')
        sys.exit(0)
      
      if __name__ == '__main__':
        Main()
      

      chrome扩展是2个文件放在一个本地目录中:

      manifest.json

      {
        "manifest_version": 2,
      
         "background": {
            "persistent": false,
            "scripts": [ "bg.js" ]
         },
      
        "name": "JNLP Fixer",
        "description": "Handle JNLPs",
        "version": "1.0",
      
        "permissions": [
          "downloads", "nativeMessaging"
        ]
      }
      

      和 bg.js(根据需要编辑主机过滤器)

      chrome.downloads.onCreated.addListener(function(downloadId) {
          var expr = /\.jnlp$/;
          //this is to limit where we apply the auto-launch.
          //for our use, i only wanted it for internal jnlps.
          var hostExpr = /(http|https):\/\/internal.company.com\//;
          if (hostExpr.test(downloadId.url)) {
              if (downloadId.state == "in_progress") {
                  console.log(downloadId.url);
                  chrome.downloads.cancel(downloadId.id,function() {
                      console.log("cancelled");
                  });
                  chrome.runtime.sendNativeMessage("com.hcs.jnlplauncher", 
                                                   {url:downloadId.url}, 
                                                   function(response) 
                                                   {
                          console.log(chrome.runtime.lastError);
                          console.log(response);
                          }
                      );
              }
          }
      
      })
      

      将 manifest.json 和 bg.js 放在一个文件夹中,并在 chrome://extensions 下以开发者模式将其作为 Unpacked 扩展加载到 chrome 中

      从 chrome://extensions 页面获取扩展程序的 ID。

      接下来是扩展和shell脚本之间的桥梁。

      文件:com.hcs.jnlplauncher.json

      {
        "name": "com.hcs.jnlplauncher",
        "description": "JNLP Launcher",
        "path": "/usr/local/bin/jnlp-launcher",
        "type": "stdio",
        "allowed_origins": [
          "chrome-extension://iacomlhfiphkdfjjjmlgckdkhmkhkibe/"
        ]
      }
      

      将其放在“~/.config/google-chrome/NativeMessagingHosts”下(适用于 linux)。有关 Windows 位置的信息,请参见 google。

      将上一步中的扩展 ID 放入该文件中。

      确保 javaws 在路径中。 (该 chrome 运行)。链接到 /usr/bin 是最简单的确定方法。

      点击一个 jnlp 文件并享受吧!!!没有提示,没有 ClickToOpen,Downloads 目录中也没有保存文件!

      如果有人想将这一切捆绑到一个漂亮的打包安装程序和/或 chrome 扩展程序中,请随意。请相信我 (Chris Holt -- hobie744@gmail.com) 并告诉我。乍一看,我看不出如何将 NativeMessagingHosts 块捆绑到扩展中。也许它必须是2件?这是我在 Chrome 扩展和 NativeMessaging 中的第一次冒险。大部分代码来自 API 文档和示例,并且可能存在一些错误。

      【讨论】:

      • 嗨@Chris Holt,太棒了。但是我的 chrome 仍然下载文件;但是,我现在可以在状态栏中单击下载的文件,然后它会启动 javaws,这是很大的改进 - 非常感谢。
      【解决方案4】:

      不幸的是,这是 Google Chrome 中的一个错误(/功能?)still exists,但它已部分修复:您现在可以自动打开 jnlp 文件,但它们仍保存在下载文件夹中

      • 下载 jnlp
      • 右键单击下载栏并选择始终打开此类型的文件
      • 现在点击 jnlp 直接启动它

      【讨论】:

      • 他们已经阻止了 jnlps 的“始终打开”选项。很烦人。在下面的答案中查看我沮丧的结果以获取解决方法。
      【解决方案5】:

      使用通过 web start 部署的嵌入式小程序启动 JNLP。

      1. 从一个基于 Swing 的 JApplet 开始,它接受图像路径(图标)和按钮字符串。使用 JWS 部署小程序(嵌入在链接所在的网页中)。
      2. 当用户单击按钮时,使用BasicService.showDocument(URL) 方法启动JWS(基于框架)应用程序。正如我在demo. of the BasicService..

        ..在 Java 6+ 中,显示另一个 web 启动文件的调用(例如 BasiceService.showDocument(another.jnlp))直接交给 JavaWS, 不会出现浏览器窗口。

      【讨论】:

      • 另外,Chrome 停止支持小程序。
      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2012-05-13
      • 2011-10-22
      • 2014-03-11
      相关资源
      最近更新 更多