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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:56:55+00:00 2026-06-13T10:56:55+00:00

I’m using javax.script package for running external JavaScript files within Java application. How can

  • 0

I’m using javax.script package for running external JavaScript files within Java application.

How can I import one JavaScript file into another JavaScript file, without using Java code?

  • 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-13T10:56:57+00:00Added an answer on June 13, 2026 at 10:56 am

    When you say without using java code, I am not completely sure what you mean, but this is a pure javascript that works (although it is calling java):

    importPackage(java.io);
    function loadJs(name, user) {
        println("Loading " + name);
        var f = new File(name);
        var br = new BufferedReader(new FileReader(f));
        var line = null;
        var script = "";
        while((line = br.readLine())!=null) {
            script += line;
        }
        println(script);
        eval(script);
        hello(user);
    }
    

    …proivided, of course, that I have the file named (say c:/temp/hellouser.js) with something like:

    function hello(name) { print('Hello, ' + name); }
    

    I tested the script using a groovy script:

    import javax.script.*;
    
    sem = new ScriptEngineManager();
    engine = sem.getEngineByExtension("js");
    script1 = """
    importPackage(java.io);
    function loadJs(name, user) {
        println("Loading " + name);
        var f = new File(name);
        var br = new BufferedReader(new FileReader(f));
        var line = null;
        var script = "";
        while((line = br.readLine())!=null) {
            script += line;
        }
        println(script);
        eval(script);
        hello(user);
    }
    """;
    
    engine.eval(script1);
    Object obj = engine.get("obj");
    Invocable inv = (Invocable) engine;
    inv.invokeFunction("loadJs", "c:/temp/hellouser.js", "Nicholas");
    

    and the output was:

    Loading c:/temp/hellouser.js

    function hello(name) { print(‘Hello, ‘ + name); }

    Hello, Nicholas

    I hope this is approximately what you were looking for….

    =========================== UPDATE ===========================

    Here’s a cleaned up version that extends the Rhino script engine factory (because the engine itself is final):

    import javax.script.Bindings;
    import javax.script.ScriptContext;
    import javax.script.ScriptEngine;
    import javax.script.ScriptException;
    
    import com.sun.script.javascript.RhinoScriptEngineFactory;
    
    /**
     * <p>Title: LoadEnabledRhinoEngineFactory</p>
     * <p>Description: Adding a loadJs function to the standard JS engine</p> 
     * <p>Company: Helios Development Group LLC</p>
     * @author Whitehead (nwhitehead AT heliosdev DOT org)
     * <p><code>org.helios.apmrouter.js.LoadEnabledRhinoEngineFactory</code></p>
     */
    
    public class LoadEnabledRhinoEngineFactory extends RhinoScriptEngineFactory {
    
        /** The load script source */
        public static final String LOAD_JS = 
            "importPackage(java.io); " +
            "var script = ''; " +
            "var ctx = null; " +
            "function loadScript(name) { " +
                "var f = new File(name); " +
                "var br = new BufferedReader(new FileReader(f)); " +
                "var line = null; " +           
                "while((line = br.readLine())!=null) { " +
                    "    script += line; " +
                "} " +
                "_e_ngine.eval(script);" + 
            "} ";
        
        /**
         * {@inheritDoc}
         * @see com.sun.script.javascript.RhinoScriptEngineFactory#getScriptEngine()
         */
        @Override
        public ScriptEngine getScriptEngine() {
            ScriptEngine se = super.getScriptEngine();
            Bindings b = se.createBindings();
            b.put("_e_ngine", se);
            se.setBindings(b, ScriptContext.GLOBAL_SCOPE);
            try {
                se.eval(LOAD_JS);
            } catch (ScriptException e) {
                throw new RuntimeException(e);
            }
            return se;
        }
        
    

    Now, loadScript(fileName) is part of the engine and you can cleanly call it with JS like:

    loadScript('c:/temp/hellouser.js'); 
    hello('Nicholas');"
    

    or as I tested in Java:

    ScriptEngine se = new LoadEnabledRhinoEngineFactory().getScriptEngine();
    try {
        se.eval("loadScript('c:/temp/hellouser.js'); hello('Nicholas');");
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    

    Cheers.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm making a simple page using Google Maps API 3. My first. One marker
I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.