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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:43:29+00:00 2026-06-11T19:43:29+00:00

How would one include a script Bar from within another script Foo that is

  • 0

How would one include a script “Bar” from within another script “Foo” that is being evaluated by the Rhino Engine, running in Java.

IE, setup the script engine like this in Java:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
BufferedReader br = new BufferedReader(new FileReader(new File("Foo.js")));
engine.eval(br);

… and put the following in Foo:

load(["Bar.js"])

According to Rhino shell documentation, that’s the way to do it. But when running from Java, it’s clearly not implemented:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "load" is not defined.

@Phillip:

Thanks for the creative response. Your solution as is doesn’t quite work because eval doesn’t make the parsed function available unless the Invocable interface is used. Further, it doesn’t work in cases where the Java code is not accessible. I elaborated on your workaround and created something that will work in pure JS:

First create an engine, exposing the Invocable interface:

var engine = new Packages.javax.script.ScriptEngineManager().getEngineByName("javascript");
engine = Packages.javax.script.Invocable(engine);

Then “load()” the script(s)

engine.eval(new java.io.FileReader('./function.js'));

At this point, functions can be evaluated with:

engine.invokeFunction("printHello", null);
  • 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-11T19:43:30+00:00Added an answer on June 11, 2026 at 7:43 pm

    I managed to do it by passing the engine to itself and then calling eval() on it inside the Javascript code:

    Java code:

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    engine.put("engine", engine);
    engine.eval(new FileReader("test.js"));
    

    test.js:

    engine.eval(new java.io.FileReader('function.js'));
    printHello();
    

    function.js:

    function printHello() {
        print('Hello World!');
    }
    

    output:

    Hello World!
    

    I don’t know if that’s the most elegant way to do it, but it works.

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

Sidebar

Related Questions

Why would $_SERVER['PHP_SELF'] return a filename in one instance as /test/foo.bar and another instance
How would I include a page from the public folder in one of my
in PHP, how would one return from an included script back to the script
I have no idea why any one would do this (include support for Lists
How would one enumerate the installed browsers on an OS X system from a
How would one switch a public bool to true from a child form in
I have an AJAX script that receives a string from a mySQL query returned
I currently have a single jQuery script page that I include in my ScriptManager
How would one go about using php include() with GET paramters on the end
Is it possible to include content within a doxygen comment block that will be

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.