gpt4 book ai didi

在浏览器中运行时,Java Web Start 不会将参数传递给应用程序

转载 作者:行者123 更新时间:2023-11-30 04:37:59 25 4
gpt4 key购买 nike

我在向使用 Java Web Start 启动的 Java 应用程序传递参数时遇到问题。我的系统是 Ubuntu Linux 12.04.1 Precise,但在 Windows 7 中也会发生同样的情况。两者都使用 Oracle Java 版本 1.7.0_09。

编辑:我正在调查 Web Start 应用程序是否比普通小程序更能满足我们的需求。小程序的问题是,如果用户离开页面(这不是我们想要的),小程序就会关闭。 (我们也有兴趣看看是否可以通过使用 Web Start 而不是小程序来解决某个安全问题。)我们希望从浏览器启动应用程序,以便我们可以将有关经过身份验证的用户的信息传递给Java 应用程序。要求用户登录应用程序似乎是一个糟糕的解决方案(我们可能必须支持 OAuth 之类的东西)。

我有一个示例程序CommandLineArgs.java:

public class CommandLineArgs {
public static void main(String[] args) {
System.out.println(String.format("Got %d command line args:", args.length));
for (String arg : args) {
System.out.println(arg);
}
}
}

我将其打包到 jar 中:

javac CommandLineArgs.java
zip cmd.jar CommandLineArgs.class

然后我有一个名为 cmd.jnlp 的 JNLP 文件:

<?xml version="1.0" encoding="utf-8"?>

<!-- Empty codebase means use same directory. -->
<jnlp spec="1.0+" codebase="https://localhost:9876/">
<information>
<title>Command Line Args Printer</title>
<vendor>No one</vendor>
<homepage href="https://localhost:9876/"/>
<description>Application that prints the command line arguments that it gets.</description>
</information>
<resources>
<j2se version="1.6+" initial-heap-size="32m" max-heap-size="128m" />
<property name="jnlp.versionEnabled" value="true"/>

<jar href="cmd.jar" main="true"/>

</resources>
<application-desc main-class="CommandLineArgs">
<!-- Here are sample arguments I'd like to pass to the program. -->
<argument>arg1</argument>
<argument>arg2</argument>
<argument>arg3</argument>
</application-desc>
</jnlp>

这是我用于测试的 HTML 页面 cmd.html。 div 元素最初是使用deployjava.js 创建的:deployJava.launchWebStartApplication('https://localhost:9876/cmd.jnlp')

<!DOCTYPE html>

<html>
<head>
<title>Java web start command line arguments test</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<div id="div1" style="position: relative; left: -10000px; margin: 0px auto; " class="dynamicDiv">
<embed type="application/x-java-applet;jpi-version=1.7.0_09" width="0" height="0" launchjnlp="https://localhost:9876/cmd.jnlp" docbase="https://localhost:9876/">
</div>

</body>
</html>

运行 https 服务器的最快方法是使用 Linux 中常见的 openssl 工具。当像这样运行服务器时,当前目录应该包含 cmd.html、cmd.jar 和 cmd.jnlp 。

sudo cp /etc/ssl/private/ssl-cert-snakeoil.key .
sudo chmod 666 ssl-cert-snakeoil.key
openssl req -batch -new -x509 -key ssl-cert-snakeoil.key -out ssl-cert-snakeoil.key.crt
openssl s_server -cert ssl-cert-snakeoil.key.crt -key ssl-cert-snakeoil.key -accept 9876 -WWW

现在,如果我浏览到 https://localhost:9876/cmd.html 我就可以运行该应用程序。 Java 控制台打开并打印此内容。请注意,有 0 个命令行参数。

   JNLP Ref (absolute): https://localhost:9876/cmd.jnlp
Match: beginTraversal
Match: digest selected JREDesc: JREDesc[version 1.6+, heap=33554432-134217728, args=null, href=null, sel=false, null, null], JREInfo: JREInfo for index 0:
platform is: 1.7
product is: 1.7.0_09
location is: http://java.sun.com/products/autodl/j2se
path is: /opt/jre1.7.0_09/bin/java
args is: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8001
native platform is: Linux, amd64 [ x86_64, 64bit ]
JavaFX runtime is: JavaFX 2.2.3 found at /opt/jre1.7.0_09/
enabled is: true
registered is: true
system is: true

Match: ignoring maxHeap: 134217728
Match: selecting InitHeap: 33554432
Match: digesting vmargs: null
Match: digested vmargs: [JVMParameters: isSecure: true, args: ]
Match: JVM args after accumulation: [JVMParameters: isSecure: true, args: ]
Match: digest LaunchDesc: null
Match: digest properties: []
Match: JVM args: [JVMParameters: isSecure: true, args: ]
Match: endTraversal ..
Match: JVM args final: -Xms32m
Match: Running JREInfo Version match: 1.7.0.09 == 1.7.0.09
Match: Running JVM args match: have:<-Xms32m satisfy want:<-Xms32m>
Got 0 command line args:

另一方面,如果我从命令行运行 javaws (javaws cmd.jnlp),我会在 java 控制台中得到此信息。现在有 3 个命令行参数。

    Match: beginTraversal
Match: digest selected JREDesc: JREDesc[version 1.6+, heap=33554432-134217728, args=null, href=null, sel=false, null, null], JREInfo: JREInfo for index 0:
platform is: 1.7
product is: 1.7.0_09
location is: http://java.sun.com/products/autodl/j2se
path is: /opt/jre1.7.0_09/bin/java
args is: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8001
native platform is: Linux, amd64 [ x86_64, 64bit ]
JavaFX runtime is: JavaFX 2.2.3 found at /opt/jre1.7.0_09/
enabled is: true
registered is: true
system is: true

Match: ignoring maxHeap: 134217728
Match: selecting InitHeap: 33554432
Match: digesting vmargs: null
Match: digested vmargs: [JVMParameters: isSecure: true, args: ]
Match: JVM args after accumulation: [JVMParameters: isSecure: true, args: ]
Match: digest LaunchDesc: null
Match: digest properties: []
Match: JVM args: [JVMParameters: isSecure: true, args: ]
Match: endTraversal ..
Match: JVM args final: -Xms32m
Match: Running JREInfo Version match: 1.7.0.09 == 1.7.0.09
Match: Running JVM args match: have:<-Djnlp.versionEnabled=true -Xms32m satisfy want:<-Xms32m>
Got 3 command line args:
arg1
arg2
arg3

我做错了什么吗?为什么在浏览器中运行时参数没有传递给我的程序?

我找到了帖子Java WS application ignoring arguments sporadically这似乎描述了同样的问题。 posdef 的解决方案是从 jnlp 文件中的 jnlp 元素中删除 href 属性,但我没有该属性。

最佳答案

application-desc 用于 WebStart 应用程序,使用 applet-desc 用于小程序。

关于在浏览器中运行时,Java Web Start 不会将参数传递给应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com