anon
anon

Reputation:

Running a Java function from a webpage?

So my friend keeps making requests about this plugin I am writing for Bukkit and, like the decent person I am, I accept them. Unfortunately I am not that great of a programmer. I am making a help page for this plugin - A local file that comes with the plugin - and I need to know how I can run the .jar file of the plugin. I have the main class all ready, and they way I plan to do it is have something like this as part of the <head></head> tags.

<script> <!--
if(confirm("Would you like to run the plugin to set up first? (Click cancel if you do not)")) {
    //Something to run the plugin .jar file
}
//-->
</script>

Would this work? If I need to make any modifications, just let me know. I think I may only be able to call functions, in which case I will just call main().
NOTE: This works as-is, I just have it make another popup instead of running the .jar file. NOTE: Both the webpage and the jarfile are already downloaded onto the user's computer, I just need to be able to run it.
NOTE: This webpage is literally an HTML file that is downloaded to their computer when they download the plugin, and I want it to run my plugin because that it an automated setup function. The website is NOT hosted on a server. I feel like I REALLY need to clarify that.

Looked through the code and decided that a link would be easiest and best for my purposes - Thanks for all the help! (By the way, it turned out that I would have the .jar file unpack the help document into the folder. Oh well :P)

Upvotes: 1

Views: 1818

Answers (3)

Markus A.
Markus A.

Reputation: 12742

Another thing you can look into is the Java Web Start framework: http://en.wikipedia.org/wiki/Java_Web_Start

This allows users to launch an arbitrary jar file directly from their browsers and optionally install it on their machines. So, if all you are looking for is a way to deploy your Java program via a website, this might be the best solution.

In this scenario, the Java code would not run "inside" the browser (well, inside the JVM, but tied to the browser), like an applet would, but instead it would run as a standard separate process as if you had launched the jar file from your computer's hard-disk.

Upvotes: 1

Markus A.
Markus A.

Reputation: 12742

If you simply put a static <applet> or <object> tag into your html page somewhere, it will always load your java code.

If you would instead like to load it dynamically later or give the user a choice about whether to load it or now, you can use the following construct in your JavaScript:

var app = document.createElement('applet');
app.archive= 'Java.jar';  // Your java jar file here
app.code= 'Java.class';   // Your Main class that extends Applet
app.width = '400';        // The size of the area for displaying ...
app.height = '10';        //   ... the applet's graphical output
document.getElementsByTagName('body')[0].appendChild(app);

(Adapted from Beachhouse's answer here: How can I embed a Java applet dynamically with Javascript?; this uses the <applet> tag, but can be easily changed to using the <object> tag instead).

It is important that your Java code actually extends java.applet.Applet. Simply having a main() function isn't enough in this scenario. The Applet class then has methods you override (init(), destroy(), start(), and stop()), which get called automatically when your applet is loaded/unloaded/shown/hidden. You can also define public functions on the class that implements Applet, which you can then call on the app object from JavaScript directly. You might have to play around with this a little, because there are some differences in the types that Java and JavaScript use, so you can't just return or expect any type of parameter, but with a little trial and error (or reading up on the web), it works pretty well.

One thing you need to keep in mind is that it takes a while to load the applet. In fact, if the browser blocks the applet or the user clicks the no-button on the permission question, it might never get loaded. So you probably won't be able to call functions on the app object immediately. Instead, write yourself a little timer that checks periodically whether your applet has loaded. What I usually do is to define a public boolean isReady() function in my applet class and then use the following Javascript:

if (typeof app.isReady=='undefined') return false;
return app.isReady();

But, of course, if you are only using the init() and start() methods without calling the applet from Javascript, you won't have to worry about this.

Upvotes: 1

woodykiddy
woodykiddy

Reputation: 6455

I think you might be looking for <applet> tag here.

<APPLET code="MyJAVAFile.jar"></APPLET>

For HTML5, you should use <object> tag, something like this:

<object type="application/x-java-applet" code="MyJAVAFile.jar"></object>

You might also find this article interesting. Worth reading.

Upvotes: 0

Related Questions