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 8103197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:28:26+00:00 2026-06-05T23:28:26+00:00

Suppose I have a Javascript file function js_main(args){ /* some code */ var x

  • 0

Suppose I have a Javascript file

function js_main(args){
     /* some code */
     var x = api_method1(some_argument);
     /* some code */
}

And I try to run it with javax.scripting the usual way

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
engine.eval(...);

Now the I’d like to handle the call to api_method1 in Javascript with my Java class. I’d like to have some kind of mapping/binding of calls i.e. each time the script calls api_method1(arg) a method

public Object api_method1(Object arg){ ... }

(placed in the same class as the engine) would be called.

Can I achieve this?

  • 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-05T23:28:28+00:00Added an answer on June 5, 2026 at 11:28 pm
    1. use engine.createBindings() to make a Bindings object;
    2. put an object exposing your method into the bindings with some name:

      Bindings b = engine.createBindings();
      b.put("api", yourApiObject);
      engine.setBindings(b, ScriptContext.ENGINE_SCOPE);
      
    3. Then in JavaScript there’ll be a global “api” object you can call:

      api.method1( "foo", 14, "whatever" );
      

    The facility is easy to use, but be careful with what you pass back and forth; it doesn’t do that much to convert JavaScript types to Java types.

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

Sidebar

Related Questions

Suppose I have 1) a HTML document. 2) This HTML document loads Javascript file
I have the following prototype JavaScript code Event.observe( window, 'load', function() { Event.observe( 'agreement',
I have small bug (I suppose) in my Javascript file. It is simple file
Suppose I have a javascript object like this: window.config config.UI = { opacity: {
Possible Duplicate: Pick random property from a Javascript object suppose I have a javascript
Suppose you have a subsystem that does some kind of work. It could be
I have some ajax calls in multiple JavaScript functions. Each one does a post
Here is my $.getJSON function (this is suppose to hit a .ashx file and
I have a javascript file that reads another file which may contain javascript fragments
I have some code which preloads images independent of their extension and acts in

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.