简体   繁体   English

如何从本地超链接调用Java程序?

[英]How to call a Java program from a hyperlink locally?

I have a html file, there are links in it, I wonder if there is a way to use those links to call a java program to generate another html file ? 我有一个html文件,其中有链接,我想知道是否有一种方法可以使用这些链接来调用java程序来生成另一个html文件?

Something like this : 像这样的东西:

<Html>
<body>
Some text
<A Href=[somehow point to a java .class file to run]>My Link</A>
More text
</Body>
</Html>

I know how to use Java to generate html, what I'm asking here is how to pass a parameter to this local java class so that it can generate html file with the input ? 我知道如何使用Java生成html,我在这里要问的是如何将参数传递给此本地java类,以便它可以使用输入生成html文件?

So if the Java program is called : MyHtmlGen.java 因此,如果Java程序名为:MyHtmlGen.java

Then the class will be MyHtmlGen.class 然后,该类将为MyHtmlGen.class

And then if I run it from the command line, it would be like this : 然后,如果我从命令行运行它,它将像这样:

> java MyHtmlGen my_input

But I don't know how to turn that into the html link above ? 但是我不知道如何把它变成上面的html链接?

That's just impossible. 那是不可能的。 A link <a> will fire a GET request to the server for the URI set in the href attribute, it's not meant to execute a specific piece of code. 链接<a>将向服务器发出针对href属性中设置的URI的GET请求,但这并不意味着要执行特定的代码段。 If you want to execute code when clicking a link, use JavaScript, but be aware that JavaScript cannot start an instance of JVM and run your exact Java application. 如果要在单击链接时执行代码,请使用JavaScript,但请注意JavaScript无法启动JVM实例并运行您的确切Java应用程序。

On the other hand, maybe you should look into Applet or JavaFX and embed the java application in your page. 另一方面,也许您应该研究Applet或JavaFX并将Java应用程序嵌入页面中。 Or probably you may submit an action to the server, and at server side you may start the JVM and execute your Java code. 也许您可以向服务器提交操作,并且可以在服务器端启动JVM并执行Java代码。

You could use WebStart to launch an Java application from a browser interaction. 您可以使用WebStart通过浏览器交互来启动Java应用程序。

The section of the linked documentation titled: "Running a Java Web Start Application From a Browser" provides a demo you can try. 链接文档的标题为“从浏览器运行Java Web Start应用程序”的部分提供了可以尝试的演示。 The link to the Java application is provided as: 提供给Java应用程序的链接为:

<a href="/some/path/Notepad.jnlp">Launch Notepad Application</a>

That documentation states that when you click the link: 该文档指出,当您单击链接时:

Java Web Start software loads and runs the application based on instructions in the JNLP file. Java Web Start软件根据JNLP文件中的指示加载并运行应用程序。

That isn't the behavior I get on Safari 7.1 on OS X 10.9 with Oracle Java 8u40 installed. 不是我在装有Oracle Java 8u40的OS X 10.9上的Safari 7.1上获得的行为。 Instead, I just get the jnlp file downloaded and can double click on the downloaded file to run the application. 相反,我只是下载了jnlp文件,并且可以双击下载的文件来运行该应用程序。 I think on some browsers, Oracle may provide a plugin to the browser which is able to launch the jnlp referenced application automatically without the user having to also double click on a downloaded jnlp file. 认为在某些浏览器上,Oracle可能会为浏览器提供一个插件,该插件能够自动启动jnlp引用的应用程序,而用户不必双击下载的jnlp文件。 Perhaps if the Java deployment toolkit were used, rather than a straight a href link, the user experience might be a bit more seamless. 也许,如果使用Java部署工具包 ,而不是使用直接a href链接,则用户体验可能会更加无缝。

Note: browser manufacturers have been phasing out support for plugin technology like this, so the experience or even the ability to automatically run the referenced application may vary for both you and your users. 注意:浏览器制造商一直在逐步停止对此类插件技术的支持,因此您和您的用户的体验甚至是自动运行引用的应用程序的能力都可能不同。 Additionally, allowing such plugins to run within a browser environment can increase the security attack vulnerability surface for a user's browser. 此外,允许此类插件在浏览器环境中运行可以增加用户浏览器的安全攻击漏洞范围。 WebStart is also quick a tricky technology to use and support for your users. WebStart也是一项快速棘手的技术,可以为您的用户使用和支持。 So for these reasons I normally don't recommend using WebStart as a deployment solution. 因此,由于这些原因,我通常不建议将WebStart用作部署解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM