Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:27:52+00:00 2026-06-06T14:27:52+00:00

I am trying to call a java script function from java code. Here is

  • 0

I am trying to call a java script function from java code.

Here is my Java code

    public static void main(String[] args) throws FileNotFoundException {
    try {
        /**
         * To call a anonymous function from java script file
         */
        ScriptEngine engine = new ScriptEngineManager()
                .getEngineByName("javascript");
        FileReader fr = new FileReader("src/js/MySpec.js");
        engine.eval(fr);

    } catch (ScriptException scrEx) {
        scrEx.printStackTrace();
    }
}

Here is my java script file:

(function() {
  alert("Hello World !!!");
})();

But when I run main method of driver class it is giving me error as below:

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "alert" is not defined. (<Unknown source>#2) in <Unknown source> at line number 2
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at Java6RhinoRunner.load(Java6RhinoRunner.java:42)
at Java6RhinoRunner.main(Java6RhinoRunner.java:12)

What I know is that it need some script engine to execute it.

For that I added rhino.jar file in to my class path.But this is not working.

I an not getting how to solve this error.
Please help.Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:27:54+00:00Added an answer on June 6, 2026 at 2:27 pm

    alert is not part of JavaScript, it’s part of the window object provided by web browsers. So it doesn’t exist in the context you’re trying to use it in. (This is also true of setInterval, setTimeout, and other timer-related stuff, FYI.)

    If you just want to do simple console output, Rhino provides a print function to your script, so you could replace alert with print. Your script also has access to all of the Java classes and such, so for instance java.lang.System.out.println('Hello'); would work from your JavaScript script (although it’s a bit redundant with the provided print function). You can also make Java variables available to your script easily via ScriptEngine.put, e.g:

    engine.put("out", System.out);
    

    …and then in your script:

    out.println('Hello from JavaScript');
    

    …so that’s a third way to do output from the script. 🙂

    See the discussion in the javax.script package documentation, in particular ScriptEngine#put, or for more complex cases, Bindings (and SimpleBindings) and ScriptContext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to call my function named isUrgencyTypeValid from my javascript code but
i am trying to call a server side button click method from javascript function
I am trying to call my Javascript method from my java in GWT below
I am trying to call a javascript function from asp.net codebehind using onclientclick for
I am trying to call a function in another .php file from this one
I'm trying to call a controller method from a javascript function, I read that
I am trying to call the __doPostback javascript function in a asp.net page from
here is my project hirarchy: from browser.js I'm trying to call ManagerController: $(#jstree).jstree({ json_data:
I'm trying to call a php function from jquery function to calculate some values
I'm trying to call a function in my ActionScript from my JavaScript, I've managed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.