Frank
Frank

Reputation: 31096

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 ?

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 ?

So if the Java program is called : MyHtmlGen.java

Then the class will be 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 ?

Upvotes: 0

Views: 4343

Answers (2)

jewelsea
jewelsea

Reputation: 159486

You could use WebStart to launch an Java application from a browser interaction.

The section of the linked documentation titled: "Running a Java Web Start Application From a Browser" provides a demo you can try. The link to the Java application is provided as:

<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.

That isn't the behavior I get on Safari 7.1 on OS X 10.9 with Oracle Java 8u40 installed. Instead, I just get the jnlp file downloaded and can double click on the downloaded file to run the application. 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. Perhaps if the Java deployment toolkit were used, rather than a straight a href link, the user experience might be a bit more seamless.

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. So for these reasons I normally don't recommend using WebStart as a deployment solution.

Upvotes: 2

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

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. 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.

On the other hand, maybe you should look into Applet or JavaFX and embed the java application in your page. Or probably you may submit an action to the server, and at server side you may start the JVM and execute your Java code.

Upvotes: 2

Related Questions