{"id":15631898,"url":"https://github.com/ianwalter/rhino-stock-example","last_synced_at":"2026-05-18T03:01:47.555Z","repository":{"id":6878133,"uuid":"8127339","full_name":"ianwalter/rhino-stock-example","owner":"ianwalter","description":"An example of how to use Mozilla Rhino to execute JavaScript within Java","archived":false,"fork":false,"pushed_at":"2013-02-10T20:30:21.000Z","size":1168,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-25T09:37:35.331Z","etag":null,"topics":["evaluation","example","java","javascript","mozilla-rhino","rhino"],"latest_commit_sha":null,"homepage":"http://www.iankwalter.com","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ianwalter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-10T20:07:08.000Z","updated_at":"2017-05-12T23:10:25.000Z","dependencies_parsed_at":"2022-08-26T09:22:34.195Z","dependency_job_id":null,"html_url":"https://github.com/ianwalter/rhino-stock-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ianwalter/rhino-stock-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianwalter%2Frhino-stock-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianwalter%2Frhino-stock-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianwalter%2Frhino-stock-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianwalter%2Frhino-stock-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ianwalter","download_url":"https://codeload.github.com/ianwalter/rhino-stock-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianwalter%2Frhino-stock-example/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33163413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T22:39:12.733Z","status":"online","status_checked_at":"2026-05-18T02:00:06.436Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evaluation","example","java","javascript","mozilla-rhino","rhino"],"created_at":"2024-10-03T10:41:59.564Z","updated_at":"2026-05-18T03:01:47.508Z","avatar_url":"https://github.com/ianwalter.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Original article at: [Executing JavaScript Within Java With Mozilla Rhino](http://www.iankwalter.com/blog/2013/02/09/executing-javascript-within-java-with-mozilla-rhino/)\n\nIf you need your Java project to be more dynamic, take a look at Rhino. Developed by Mozilla, Rhino allows you to execute and interact with JavaScript code from within Java. Here I'll show you an example of how you can take a Java object and modify that object using JavaScript!\n\nHere is our Stock object that will model information necessary for us to determine if it is undervalued:\n\n```java\n  package com.iankwalter.rhinostockexample;\n\n  /**\n   * Models a Stock for the purpose of this example.\n   *\n   * @author Ian Kennington Walter\n   */\n  public class Stock {\n\n      Double netIncome;\n      Double totalDebt;\n      Double totalCash;\n      Double marketCap;\n      boolean isUndervalued;\n\n      public Double getNetIncome() {\n          return netIncome;\n      }\n\n      public void setNetIncome(Double netIncome) {\n          this.netIncome = netIncome;\n      }\n\n      public Double getTotalDebt() {\n          return totalDebt;\n      }\n\n      public void setTotalDebt(Double totalDebt) {\n          this.totalDebt = totalDebt;\n      }\n\n      public Double getTotalCash() {\n          return totalCash;\n      }\n\n      public void setTotalCash(Double totalCash) {\n          this.totalCash = totalCash;\n      }\n\n      public Double getMarketCap() {\n          return marketCap;\n      }\n\n      public void setMarketCap(Double marketCap) {\n          this.marketCap = marketCap;\n      }\n\n      public boolean isUndervalued() {\n          return isUndervalued;\n      }\n\n      public void setUndervalued(boolean undervalued) {\n          isUndervalued = undervalued;\n      }\n  }\n```\n\nHere is some JavaScript code that will evaluate whether a Stock is undervalued:\n\n```javascript\n  var earnings = stock.getNetIncome() * 10;\n  earnings += (stock.getTotalCash() - stock.getTotalDebt());\n  if (earnings \u003e stock.getMarketCap()) {\n    stock.setUndervalued(true);\n  } else {\n    stock.setUndervalued(false);\n  }\n```\n\nHere is the Java code that will create the Stock object and perform the evaluation of that Stock using Rhino:\n\n```java\n  package com.iankwalter.rhinostockexample;\n\n  import org.mozilla.javascript.Context;\n  import org.mozilla.javascript.Scriptable;\n  import org.mozilla.javascript.ScriptableObject;\n\n  /**\n   * An example of how to use Mozilla Rhino to execute JavaScript within Java\n   *\n   * @author Ian Kennington Walter\n   */\n  public class EvaluateStock {\n\n      /**\n       * Evaluates whether a Stock is undervalued based on logic within a JS script\n       *\n       * @param args\n       */\n      public static void main(String[] args) {\n\n          // Define evaluation JavaScript. Typically this would be stored in a file or a database.\n          String evaluationScript =\n              \"var earnings = stock.getNetIncome() * 10; \" +\n              \"earnings += (stock.getTotalCash() - stock.getTotalDebt()); \" +\n              \"if (earnings \u003e stock.getMarketCap()) { \" +\n              \"    stock.setUndervalued(true); \" +\n              \"} else { \" +\n              \"    stock.setUndervalued(false); \" +\n              \"} \";\n\n          // Create a Stock object to evaluate.\n          Stock stock = new Stock();\n          stock.setNetIncome(10.0);\n          stock.setTotalCash(100.0);\n          stock.setTotalDebt(0.0);\n          stock.setMarketCap(150.0);\n\n          // Create and enter a Context. A Context stores information about the execution environment of a script.\n          Context cx = Context.enter();\n          try {\n              // Initialize the standard objects (Object, Function, etc.). This must be done before scripts can be\n              // executed. The null parameter tells initStandardObjects\n              // to create and return a scope object that we use\n              // in later calls.\n              Scriptable scope = cx.initStandardObjects();\n\n              // Pass the Stock Java object to the JavaScript context\n              Object wrappedStock = Context.javaToJS(stock, scope);\n              ScriptableObject.putProperty(scope, \"stock\", wrappedStock);\n\n              // Execute the script\n              cx.evaluateString(scope, evaluationScript, \"EvaluationScript\", 1, null);\n          } catch (Exception e) {\n              e.printStackTrace();\n          } finally {\n              // Exit the Context. This removes the association between the Context and the current thread and is an\n              // essential cleanup action. There should be a call to exit for every call to enter.\n              Context.exit();\n          }\n\n          // Output whether the stock was determined to be undervalued.\n          System.out.println(\"Is stock undervalued?\");\n          System.out.println(stock.isUndervalued());\n      }\n\n  }\n```\n\nOutput:\n\n    Is stock undervalued?\n    true\n\nSuccess!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianwalter%2Frhino-stock-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fianwalter%2Frhino-stock-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianwalter%2Frhino-stock-example/lists"}