-1

I am trying to use below command in my javascript,

var file = new Java.type("java.io.File");

but i get an error

Uncaught ReferenceError: Java is not defined

Could anybody help me, how can i use this javalibrary in my code. I am using it for like file.listfiles() method

Thanks in advance.

  • 3
    You cannot. [Java is not JavaScript.](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Bergi Jun 17 '20 at 21:06
  • Java isn't Javascript, if you would describe what you want to do in more detail (and in what environment), you might get a better answer – Icepickle Jun 17 '20 at 21:06
  • 1
    Except for the "Java" part, both languages have nothing to do with each other. Absolutely nothing. Also, Javascript is a ducktyped language, which means type is only known at runtime, and can be switched to whatever any time. Furthermore, Javascript cannot access the file system for security reasons. No JS library can do that for you. – connexo Jun 17 '20 at 21:08

1 Answers1

1

It sounds like you are talking about this Java feature.

You need to use a JavaScript engine that exposes the Java Scripting API through the Java global.

I'm not aware of any modern JavaScript browser runtime which does this. (I think if you go back to Netscape Navigator 4 you will find support). You will find support if you use Rhino to execute the JS instead of a web browser.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335