diff --git a/nashorn/make/build.xml b/nashorn/make/build.xml
index bbc6f7cebd5..0ece37f415f 100644
--- a/nashorn/make/build.xml
+++ b/nashorn/make/build.xml
@@ -492,6 +492,10 @@ grant codeBase "file:/${basedir}/test/script/markdown.js" {
permission java.io.FilePermission "${basedir}/test/script/external/showdown/-", "read";
};
+grant codeBase "file:/${basedir}/test/script/basic/JDK-8158467.js" {
+ permission java.lang.RuntimePermission "nashorn.setConfig";
+};
+
\/
diff --git a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
index 71c6d7927f9..9d51ed4a093 100644
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
@@ -1166,7 +1166,17 @@ public final class Context {
}
// Try finding using the "app" loader.
- return Class.forName(fullName, true, appLoader);
+ if (appLoader != null) {
+ return Class.forName(fullName, true, appLoader);
+ } else {
+ final Class> cl = Class.forName(fullName);
+ // return the Class only if it was loaded by boot loader
+ if (cl.getClassLoader() == null) {
+ return cl;
+ } else {
+ throw new ClassNotFoundException(fullName);
+ }
+ }
}
/**
diff --git a/nashorn/test/script/basic/JDK-8158467.js b/nashorn/test/script/basic/JDK-8158467.js
new file mode 100644
index 00000000000..c67107768d4
--- /dev/null
+++ b/nashorn/test/script/basic/JDK-8158467.js
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8158467: AccessControlException is thrown on public Java class access if "script app loader" is set to null
+ *
+ * @option -scripting
+ * @test
+ * @run
+ */
+
+var Factory = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory");
+var fac = new Factory();
+
+// This script has to be given RuntimePermission("nashorn.setConfig")
+var e = fac["getScriptEngine(java.lang.ClassLoader)"](null);
+
+print(e.eval("java.lang.System"));
+print(e.eval("({ foo: 42})").foo);
+print((e.eval("function(x) x*x"))(31));
+
+e.put("output", print);
+var runnable = e.eval(<