Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Java application on a webpage

Tags:

java

html

I have written a Java application that I would like to run inside of a web page. How do I do this?

Code is below:

class Permutations {
    static long factorial(int num){
            long factorial = 1;
            for (int forBlockvar = num; forBlockvar > 1; forBlockvar--) {
                    factorial = factorial * forBlockvar;
            }
            return factorial;
    }

    public static void main(String[] args){
            long FactNmR;
            int n = 10;
            int num = n;
            int r = 4;
            int nMr = n - r;
            long FactN = factorial(num);
            if (nMr <= 1){
                    FactNmR = 1;
            }
            else  {
                    num = nMr;
                    FactNmR = factorial(num);
            }
            long permutations = FactN;
            permutations = permutations / FactNmR;
            System.out.println(permutations);
   }
}
like image 515
fr00ty_l00ps Avatar asked Dec 22 '11 15:12

fr00ty_l00ps


2 Answers

There are several way to do this with java.

One way is to do it with Java Servlet.

You need a html form with an action that points to a Servlet (an extended Java Class) Have a look at this tutorial

like image 161
Matthias Avatar answered Oct 02 '22 05:10

Matthias


Instead of running a server or applet, it is also possible to compile Java into JavaScript using JSweet.

This is JSweet's translation of your Permutations class:

 /* Generated from Java with JSweet 2.0.0 - http://www.jsweet.org */
var Permutations = (function () {
    function Permutations() {
    }
    Permutations.factorial = function (num) {
        var factorial = 1;
        for (var forBlockvar = num; forBlockvar > 1; forBlockvar--) {
            factorial = factorial * forBlockvar;
        }
        ;
        return factorial;
    };
    Permutations.main = function (args) {
        var FactNmR;
        var n = 10;
        var num = n;
        var r = 4;
        var nMr = n - r;
        var FactN = Permutations.factorial(num);
        if (nMr <= 1) {
            FactNmR = 1;
        }
        else {
            num = nMr;
            FactNmR = Permutations.factorial(num);
        }
        var permutations = FactN;
        permutations = Math.floor(permutations / FactNmR);
        console.info(permutations);
    };
    return Permutations;
}());
Permutations["__class"] = "Permutations";
Permutations.main(null);
like image 24
Anderson Green Avatar answered Oct 02 '22 03:10

Anderson Green