Yes it's possible and I can guarantee if you follow this tutorial you will be shocked about how super easy is to use the Java Virtual Machine for modern web browsers which called CheerpJ
.
CheerpJ is a WebAssembly-based Java Virtual Machine for the browser. It has extensive compatibility with Java 8 and provides a full runtime environment1 for running Java applications, applets, libraries, and Java Web Start / JNLP applications in the browser without plugins.
Here is a super easy example on JS run this:
<script src="https://cjrtnc.leaningtech.com/3.0rc2/cj3loader.js"></script>
await cheerpjInit();
const lib = await cheerpjRunLibrary("/app/example.jar");
const Example = await lib.com.example.Example;
const example = await new Example();
await example.hello();
And this is the Java method call:
package com.example;
public class Example {
public String[] greetings = {"Hello", "Bye"};
public void hello() {
System.out.println("Example says hello!");
}
}
Attention!!! the app is not a folder path in your project but instead, it refers to the root of web server for example app=http://localhost:8080/ for more info refer to this link