java 启动 webstart 而不下载...?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11679373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:01:26  来源:igfitidea点击:

Launch webstart without downloading...?

javagoogle-chromejnlpjava-web-start

提问by DankMemes

I have made a Java webstart application, and created an HTML page with the link to launch it. The problem is, in Google Chrome, there is no option to just 'Open' a file without saving it. I want to make an HTML page that can automatically launch a JNLP file without having to save it. Or rather, without the user having to open their file explorer to launch it) Is this possible?

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

采纳答案by Andrew Thompson

Launch the JNLP using an embedded applet deployed using web start.

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

  1. Start with a Swing based JApplet that accepts an image path (icon) and a string for the button. Deploy the applet (embedded in the web page, where the link would be) using JWS.
  2. When the user clicks the button, use the BasicService.showDocument(URL)method to launch the JWS (frame based) app. As I note in the demo. of the BasicService..

    ..In Java 6+, a call to show another web start launch file (e.g. BasiceService.showDocument(another.jnlp))will be handed directly to JavaWS,with no browser window appearing.

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

    ..在 Java 6+ 中,调用以显示另一个 Web 启动启动文件(例如,BasiceService.showDocument(another.jnlp))将直接传递给 JavaWS,不显示浏览器窗口。

回答by Chris Holt

After getting fed up this problem, I wrote my own work around extension.

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

It's written under ubuntu, but should be portable (even to win32 with some work/reading).

它是在 ubuntu 下编写的,但应该是可移植的(甚至可以通过一些工作/阅读移植到 win32)。

Single click launches a jnlp file without prompting, or downloading. it just passes the url for the jnlp file to javaws directly. no cluttered Downloads folder, no extra clicks.

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

It's simple, crude and effective. I filtered the URL so it would only apply to my own internal server so I don't accidentally launch some random jnlp file. Lots more could be done to improve it, I'm sure. Use AS-IS, no warranty, etc, etc.

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

The files:

文件:

/usr/local/bin/jnlp-launcher

/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()

The chrome extension is 2 files placed in a local directory:

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

manifest.json

清单文件.json

{
  "manifest_version": 2,

   "background": {
      "persistent": false,
      "scripts": [ "bg.js" ]
   },

  "name": "JNLP Fixer",
  "description": "Handle JNLPs",
  "version": "1.0",

  "permissions": [
    "downloads", "nativeMessaging"
  ]
}

And bg.js (edit as needed for host filters)

和 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);
                    }
                );
        }
    }

})

Put manifest.json and bg.js in a folder and load it as an Unpacked extension in chrome in developer mode under chrome://extensions

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

Get the ID for the extension from the chrome://extensions pages.

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

Next is the bridge between the extension and the shell script.

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

File: com.hcs.jnlplauncher.json

文件:com.hcs.jnlplauncher.json

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

Place this under "~/.config/google-chrome/NativeMessagingHosts" (for linux). see google for windows locations.

将它放在“~/.config/google-chrome/NativeMessagingHosts”下(对于 linux)。有关 Windows 位置,请参阅谷歌。

Put your extension ID from the previous step into that file.

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

Make sure javaws is in the path. (that chrome runs with). link to /usr/bin is easiest way to be sure.

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

Click on a jnlp file and enjoy!!! No prompt, no ClickToOpen, and no file saved in the Downloads directory.!

单击 jnlp 文件并享受!没有提示,没有 ClickToOpen,也没有文件保存在下载目录中。!

If someone would like to bundle this all together into a nice packaged installer and/or chrome extension feel free. Please credit me (Chris Holt -- [email protected]) and let me know. At first glance, I couldn't see how to bundle the NativeMessagingHosts piece into the extension. Perhaps it has to be 2 pieces? This is my first adventure in Chrome Extensions and NativeMessaging. Most of the code comes from the API docs and examples, and there are probably a few bugs.

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

回答by Henno Vermeulen

Unfortunately this is a bug(/feature?) in Google Chrome which still exists, however it is partly fixed: you can now automatically open jnlp files, but it they are still saved to the downloads folder

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

  • download the jnlp
  • right click in the download bar and select to always open files of this type
  • clicking the jnlp now directly launches it
  • 下载 jnlp
  • 右键单击下载栏并选择始终打开此类文件
  • 单击 jnlp 现在直接启动它

回答by Serak Shiferaw

This sample(Embedding JavaFX 2 in Swing) and articles are a great sample and they also work with modern browsers

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

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

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

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

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