0

I am trying to use Rhino to covert some JavaScript code to Java. As anyone who has any experience at all with JavaScript knows "document" is used for lots of things. So, it is used in various functions in the code I am using. However, when I execute Rhino it just gives me this error

js: uncaught JavaScript runtime exception: ReferenceError: "document" is not defined.

Does anyone know what is causing this? And if so how to fix it? I am using Rhino version 1.7 on Ubuntu 14.10 64 bit.

Or if anyone could atleast show me how to evaluate JavaScript from within Java. I have done some research, but the code never works.

crank123
  • 251
  • 1
  • 4
  • 17

2 Answers2

1

Rhino is a just interpreter with only defines base object. Object such as 'document' 'window' is external HTML DOM object. It is out of scope for Rhino, or any other Javascript interpreter (such as jscript(IE) , V8 , spidermonkey, and nashorn).

Please refer java-based HtmlUnit which also uses Rhino and it defines most of HTML DOM objects. It is updated quite well.

1

After hours of digging and research I have been able to find out how to resolve this problem...

I have done two things.

I stopped using Rhino (considering it is outdated) and I switched to Nashorn


I also found out how to implement document How to append xml nodes (as a string) into an existing XML Element node (only using java builtins)?

That took very unconventional means to find this answer, which would make any programmer go "Oh...That's one way to do it."

The way implemented in the question actually worked.

DISCLAIMER

My problems are not completely solved. Nashorn still gives me this error Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to org.w3c.dom.DocumentType But, that's another question.
Community
  • 1
  • 1
crank123
  • 251
  • 1
  • 4
  • 17