2012-07-26 42 views
5

我製作了一個Java webstart應用程序,並創建了一個帶有鏈接的HTML頁面來啓動它。問題是,在谷歌瀏覽器中,沒有選項可以在不保存的情況下「打開」文件。我想製作一個HTML頁面,可以自動啓動一個JNLP文件,而不必保存它。或者說,沒有用戶不得不打開他們的文件資源管理器來啓動它)這可能嗎?無需下載...啓動webstart ...?

+0

相關:HTTP://計算器。 com/questions/9348449/can-i-delete-downloaded-jnlp-file-java-web-start-jws-from-java-application – finnw 2012-07-27 08:46:18

回答

2

使用使用web start部署的嵌入式applet啓動JNLP。

  1. 開始與基於JApplet的一個Swing接受的圖像路徑(圖標),併爲按鈕的字符串。使用JWS部署小程序(嵌入網頁中的鏈接所在的位置)。
  2. 當用戶單擊該按鈕時,使用BasicService.showDocument(URL)方法啓動JWS(基於框架)應用程序。正如我注意到在demo. of the BasicService ..

    ..In的Java 6+,呼叫顯示其它網站開始啓動文件(例如:BasiceService.showDocument(another.jnlp))直接交給javaws的,沒有瀏覽器窗口出現。

+1

好的,但在chrome中,它並沒有交給JavaWS。它打開一個瀏覽器窗口..... – DankMemes 2012-07-27 19:08:42

+3

此外,Chrome停止支持小程序。 – 2016-03-04 13:19:46

2

不幸的是,這是在谷歌瀏覽器的錯誤(/功能?)這still exists,但它是部分固定:現在就可以自動打開JNLP文件,但他們仍然保存在下載文件夾

  • 下載JNLP
  • 在下載欄中點擊右鍵並選擇這種類型
  • 點擊JNLP現在直接的總是打開的文件啓動它
+3

他們已經阻止了jnlps的「始終打開」選項。很煩人。請在下面的答案中查看我的挫折結果,以獲得解決方法。 – 2015-10-23 04:02:37

4

受夠了這個問題之後,我寫了我自己的工作圍繞擴展。

這是在Ubuntu下編寫的,但應該是可移植的(甚至有一些工作/閱讀win32)。

單擊無需啓動或下載即可啓動jnlp文件。它只是將jnlp文件的url直接傳遞給javaws。沒有混亂的下載文件夾,沒有額外的點擊。

它簡單,粗糙,有效。我過濾了URL,所以它只適用於我自己的內部服務器,所以我不會意外啓動一些隨機jnlp文件。我敢肯定,可以做更多的事情來改善它。使用AS-IS,沒有擔保,等等,等等

的文件:

在/ usr/local/bin目錄/ JNLP - 發射

#!/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); 
        } 
       ); 
     } 
    } 

}) 

放清單。json和bg.js放在一個文件夾中,並在chrome下以開發人員模式在chrome中加載爲Unpacked擴展名。// extensions

從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 /谷歌鉻/ NativeMessagingHosts」(對於Linux)。看到谷歌的Windows位置。

將您的擴展ID從上一步放入該文件。

確保javaws在路徑中。 (該chrome運行)。鏈接到/ usr/bin是確保最簡單的方法。

點擊jnlp文件,享受!沒有提示,沒有ClickToOpen,也沒有文件保存在Downloads目錄中。

如果有人想把這些都捆綁到一個很好的打包安裝程序和/或鉻擴展免費。請相信我(克里斯霍爾特 - [email protected])並讓我知道。乍看之下,我看不出如何將NativeMessagingHosts作品捆綁到擴展中。也許它必須是2件?這是我在Chrome擴展和NativeMessaging中的第一次冒險。大部分代碼都來自API文檔和示例,並且可能存在一些錯誤。

+0

嗨@Chris Holt,太棒了。但我的Chrome還在下載文件;然而,我現在可以在狀態欄中點擊下載的文件,然後啓動javaws,大大改進 - 非常感謝。 – tink 2016-06-14 19:44:55