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

  • Home
  • SEARCH
  • 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 8294339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:10:39+00:00 2026-06-08T14:10:39+00:00

When I run this java code, i am able to get the values of

  • 0

When I run this java code, i am able to get the values of variables define outside the function, but i am unable to get the values of variable define inside the function. how to access these variable values?

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;


public class JSFunctionTest {

public static void main(String args[]) {

    String code = "var name='nc',global_a = 'jill'; " + "\n"+
            "function myfunc(b) { " + "\n"+
            "var local_a = 1;" + "\n"+
            "global_a = 'jack';" + "\n"+
            " return b;" + "\n"+
            "}"; 

    Context context = Context.enter();
    context.setGeneratingDebug(true);
    context.setOptimizationLevel(-1);

    Scriptable scope = context.initStandardObjects(); 

    context.evaluateString(scope, code, "code", 1, null);
    //getting values of varables
    System.out.println("var name:"+scope.get("name", scope));
    System.out.println("var global_a:"+scope.get("global_a", scope));
    System.out.println("var local_a:"+scope.get("local_a", scope));//not found becase function wasnt run

    //calling the function.
    Object fObj = scope.get("myfunc", scope);
    if (!(fObj instanceof Function)) {
        System.out.println("myfunc is undefined or not a function.");
    } else {
        Object functionArgs[] = { "nc" };
        Function f = (Function)fObj;
        Object r = f.call(context, scope, scope, functionArgs);
        String report = "myfunc('nc') = " + Context.toString(r);

        //trying to access global and local a after calling function
        System.out.println("var global_a:"+scope.get("global_a", scope));//values is changed, because this is defined out side the function.
        System.out.println("var local_a:"+scope.get("local_a", scope));// still not found,after running the function.

        System.out.println(report);
    }

}

}

  • 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-08T14:10:42+00:00Added an answer on June 8, 2026 at 2:10 pm

    I was able to resolve this, by implementing a debugger using the debug API in Rhino.

    1. Implement your own debugger and debugframe classes.
    2. Hook them to your context using setDebugger() method
    3. in the DebugFrame class, I have implemented. cache the scope object in the onEnter method.
    4. in the onLineChange method, you can get the local variables using ScriptableObject.getProperty() passing the cache scope object and giving the names
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This Java code compiles fine, but when I try to run it I get:
I run this piece of code on Visual C++ 2010 char c[10]; cin.get(&c[0],5); cin.get(&c[2],4);
I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I need to run this line from my c++ program: java -jar test.jar text1
OK - I know I can run my java process like this : java
This is strange to me: when I run in Java byte[] data = new
I'm trying to run a Java Jetty application and I keep seeing this error:
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
I created by own HBase java client code, but I am having a really

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.