Tencent/APIJSON · error · NullPointerException

找不到可执行 " + (isEmpty ? "js" : lang) + " 脚本的引擎!engine == null!

Error message

找不到可执行 " + (isEmpty ? "js" : lang) + " 脚本的引擎!engine == null!

What it means

After the ENABLE_SCRIPT_FUNCTION gate passes, getScriptEngine resolves the engine: an empty lang falls back to the default SCRIPT_ENGINE (usually JavaScript), otherwise SCRIPT_ENGINE_MANAGER.getEngineByName(lang). NullPointerException here means javax.script found no engine registered for that language in the JVM — typically 'js' on JDK 15+ where Nashorn was removed, or a non-JVM language (python/ruby) whose engine jar is absent from the classpath.

Source

Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java:1350

				}
			}
		}

		Log.i(TAG, "parse  return real = " + toJSONString(real));
		return real;
	}

	public static ScriptEngine getScriptEngine(String lang) {
		if (ENABLE_SCRIPT_FUNCTION == false) {
			throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION" +
					" == false 时不支持执行脚本!如需支持则设置为 true !");
		}

		boolean isEmpty = StringUtil.isEmpty(lang, true);
		ScriptEngine engine = isEmpty ? SCRIPT_ENGINE : SCRIPT_ENGINE_MANAGER.getEngineByName(lang);

		if (engine == null) {
			throw new NullPointerException("找不到可执行 " + (isEmpty ? "js" : lang) + " 脚本的引擎!engine == null!");
		}

		return engine;
	}


	/**执行操作
	 * @param opt
	 * @param targetChild
	 * @param real
	 * @param parser
	 * @return
	 * @throws Exception
	 */
	public static <T, M extends Map<String, Object>, L extends List<Object>> M operate(Operation opt, M targetChild
            , M real, @NotNull Parser<T, M, L> parser) throws Exception {
		if (targetChild == null) {
			return real;

View on GitHub (pinned to 5284052872)

Solutions

  1. On JDK 15+, add standalone Nashorn: org.openjdk.nashorn:nashorn-core:15.4 plus org.ow2.asm:asm:9.x (and asm-util/asm-tree) to the runtime classpath — SCRIPT_ENGINE_MANAGER will then find 'js'.
  2. For a non-JS language, add its JSR-223 engine jar (e.g. org.apache.groovy:groovy-jsr223, org.python:jython) and use the exact engine name ScriptEngineManager.getEngineByName expects.
  3. Verify availability at startup with a guard: ScriptEngineManager().getEngineByName(lang) != null, and fail fast/log instead of at request time.
  4. Avoid scripts entirely by registering plain Java functions via FunctionParser.

Example fix

// before (JDK 17, no js engine on classpath)
// dependencies: none -> getScriptEngine("js") throws NPE

// after (Maven)
<dependency>
  <groupId>org.openjdk.nashorn</groupId>
  <artifactId>nashorn-core</artifactId>
  <version>15.4</version>
</dependency>
<dependency>
  <groupId>org.ow2.asm</groupId>
  <artifactId>asm</artifactId>
  <version>9.6</version>
</dependency>
Defensive patterns

Strategy: validation

Validate before calling

static final boolean JS_OK;
static {
  ScriptEngine e = new ScriptEngineManager().getEngineByName("js");
  JS_OK = e != null;
}
void beforeRequest() {
  if (!JS_OK) throw new IllegalStateException(
    "No 'js' ScriptEngine on classpath — add org.openjdk.nashorn:nashorn-core (JDK 15+) or run on JDK 8-14");
}

Try / catch

try { engine = AbstractVerifier.getScriptEngine(lang); }
catch (NullPointerException npe) {
  // engine missing: report classpath/JDK mismatch instead of retrying blindly
  throw new IllegalStateException("Script engine '" + lang + "' unavailable on this JVM (" +
    System.getProperty("java.version") + ") — add the JSR-223 engine dependency", npe);
}

Prevention

When it happens

Trigger: Running on JDK 15+ (Nashorn removed) and calling any script function (lang empty or 'js' → getEngineByName returns null); or specifying lang 'python', 'kotlin', 'groovy' without the corresponding JSR-223 engine (e.g. groovy-jsr223, jython) on the classpath. Note on JDK 11-14 Nashorn is present but deprecated and warns.

Common situations: App worked on JDK 8, broke after upgrading to JDK 11+ without adding the standalone Nashorn (org.openjdk.nashorn:nashorn-core) + asm dependencies; declaring a custom lang in a function definition but forgetting its engine jar; minimal Docker images that strip optional modules.

Related errors


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/ae91f1ddd1e7c2ef. Report an issue: GitHub.