How to insert Java code into html?

Asked

Viewed 11,349 times

1

Hello guys I would like to know if it is possible to insert Java code into html,because I want to make an encryption program in Java and put inside html.

I’m asking here why I haven’t found anywhere about whether it’s possible and how to put.

2 answers

6


Before not now yes.

Until recently my answer would be :

You need to use an applet for this, because you cannot insert the Java directly in HTML. Applet support is increasing restricted and soon I think there will be no more support for Java applets (are very insecure).

But... a project I met recently changed my answer to :

Yes is possible and using Javascript!

This project is Javapoly.js more information on https://www.javapoly.com/ .

This is a library that allows you to have Java (JVM) support in browsers.

As an example you can do this in HTML:

<!-- Include the Polyfill -->
<script src="https://www.javapoly.com/javapoly.js"></script>

<!-- Write your Java code -->
<script type="text/java">
  package com.demo;
  import com.javapoly.dom.Window;

  public class Greeter
  {
    public static void sayHello(String name)
    {
      Window.alert("Hello " + name + ", from Java!");
    }
  }
</script>

<!-- Invoke your Java code from Javascript -->
<script type="text/javascript">
  com.demo.Greeter.sayHello("world");
</script>

Yes, that’s Java in HTML!

You can even import libraries :

<!-- Include the Polyfill -->
<script src="https://www.javapoly.com/javapoly.js"></script>

<!-- Include your favorite Java libraries (jar files) -->
<script type="text/java" src="http://www.yourdomain.com/jimboxutilities.jar"></script>
<script type="text/java" src="http://www.yourdomain.com/guava.jar"></script>
<script type="text/java" src="http://www.yourdomain.com/apache-commons.jar"></script>

<!-- Or, include individual .class files -->
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Foo.class"></script>
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Noise.class"></script>

<!-- Or just include the source directly -->
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Bar.java"></script>
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Girls.java"></script>

Note: Never confuse Java with Javascript https://www.java.com/pt_BR/download/faq/java_javascript.xml.

Obs 2: I never used it, it seems something recent and I don’t know what support yet. But before we answer that we can’t, I think it’s good to know that people are thinking about it.

Here’s the result of Try It Yourself that’s on the guys website. inserir a descrição da imagem aqui

  • Neither Jscript with javascript...

  • Hello friend, I tested and did not appear output in browser.

  • It appeared to me. It’s read, but it worked.

  • Opening the browser developer tool (famous F12) vi that when loading the page, after a while starts in the console window to appear some messages like : JVM 1 stdout> Java Main started&#xA;javapoly.js:19385 JVM 1 stdout> after bridge Active count:1&#xA;javapoly.js:19385 JVM 1 stdout> Compiling: [-d, /tmp/data, /tmp/data/com/javapoly/demo/HomepageDemo.java] after that after a lag I can edit the code and click the button Compile & Run. It must still be something very unstable.

  • @Falion try using Chrome and taking a little time to load Java. I think the guys could put a "Loading" on the site.

  • @Falion Does it really pay to have all this work instead of using the javaweb, which was made for this at once?

  • @Diego F Yes, I need to do it this way.

  • @Guilhermejsantos I tried to do the way you said (using Chrome) to take time to load Java and it didn’t work.

Show 3 more comments

-3

Yes. It’s called jquery. Uses. The javascript libraries . However you have to. Obey the html code structure. Include it in. Part of the tag. before vc can create Function and decision deviation with. If Else. On the body you can call these Function by clicking or moving the cursor or loading of the page in the div or object with onkeydown=funtion()? it is super powerful this tool and all its compatible with Html5 compatible with Chrome but some functions also work in IE9. You can use to facilitate bootstrap q tbm uses a lot of javascript

*Function name to be used.

  • 2

    Hello John, welcome to the SO-Pt community. In the question he refers to JAVA. jQuery in turn is for Javascript (Ecmascript). Easy to confuse, but these are extremely different things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.