-9

normally, i drag/drop any html file into (any) browser (on my macOS Catalina) and it runs javascript within the tags locally perfectly. now, progressing to importing module classes (import java.io.*;)

the code gets hung up on the import statement with: Uncaught SyntaxError: Unexpected token '.'

yes, i've added type="module" inside the script tag, prior to that i was getting: Uncaught SyntaxError: Cannot use import statement outside a module

what embarrassingly basic thing am i overlooking???

i've tried uploading to test on web hosting server, and i still same error (just any scripts containing an import statement) i've tried re-installing latest JDK 15.0.1.pkg as well

a simple example html file i'd expect to work:

<!DOCTYPE HTML>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>

<script type="module">

import java.util.Scanner;

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Enter username");

    String userName = myObj.nextLine();
    System.out.println("Username is: " + userName);
  }
}
</script>

</body>
</html>
  • 10
    Is this a joke about the fact that java is not javascript? – Federico klez Culloca Dec 30 '20 at 22:59
  • 3
    The code in this question is extremely confused as to what it is trying to do. --- Is it a standalone Java console program? If so, then a `main()` method makes sense, but the HTML doesn't. --- Is it a standalone web page *(drag'n'drop to browser says it is)*)? Then Java makes no sense at all. --- Is it a JSP file for a Servlet Container? If so, then embedded Java code is value inside `<% %>` tags, but a `main()` method makes no sense. – Andreas Dec 30 '20 at 23:06
  • ` – fantaghirocco Dec 30 '20 at 23:50

3 Answers3

4

Q: Do you honestly think you can put Java code in an HTML "<script>" tag and have the browser execute it?
A: You can't.

Q: I hope you don't think "Java" is the same as "Javascript".
A: The two are very, VERY different.
     Look here for more details: https://stackoverflow.com/a/245069/421195

NOTES:

  1. One major difference is that Javascript is a scripting language: JS text is interpreted and executed on-the-fly (e.g. by your web browser).

  2. If you wish to execute Java in a "web context", or if you wish to co-mingle Java code and HTML markup in the same source file, then one way to do this is with JSP (Java Server Pages).

I hope that helps clarify things - at least a little...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • YES! this is the direction i just needed pointing in. thanks, and again, i did say embarrassingly basic newbie issue – nuccifilms Dec 30 '20 at 23:48
0

You can't use Java into <script> tags in HTML. However, you can instead use JavaScript in a <script> tag.

jeuxjeux20
  • 391
  • 1
  • 6
  • 20
0

You can't use Java in the HTML. There is an option to do it with JSP which has the same tags that HTML and you can add Java logic. Here you have a post Use Java code in JSP

Andres Sacco
  • 650
  • 3
  • 13