changeset 9172:207af5192f54

6760902: inconsistent behavior in system class loader for classes and resources Reviewed-by: psandoz, ksrini
author mchung
date Mon, 27 Jan 2014 16:22:08 -0800
parents 3d358407b796
children 79906d84de14
files src/share/classes/sun/misc/Launcher.java test/java/lang/ClassLoader/getResource/GetResource.java test/java/lang/ClassLoader/getResource/GetResource.sh test/java/lang/ClassLoader/getResource/test.properties
diffstat 4 files changed, 176 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/misc/Launcher.java	Mon Jan 27 14:29:37 2014 +0100
+++ b/src/share/classes/sun/misc/Launcher.java	Mon Jan 27 16:22:08 2014 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2014, 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
@@ -266,7 +266,7 @@
             throws IOException
         {
             final String s = System.getProperty("java.class.path");
-            final File[] path = (s == null) ? new File[0] : getClassPath(s);
+            final File[] path = (s == null) ? new File[0] : getClassPath(s, true);
 
             // Note: on bugid 4256530
             // Prior implementations of this doPrivileged() block supplied
@@ -322,7 +322,7 @@
          * This class loader supports dynamic additions to the class path
          * at runtime.
          *
-         * @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch
+         * @see java.lang.instrument.Instrumentation#appendToSystemClassLoaderSearch
          */
         private void appendToClassPathForInstrumentation(String path) {
             assert(Thread.holdsLock(this));
@@ -364,7 +364,8 @@
                 urls = AccessController.doPrivileged(
                     new PrivilegedAction<URL[]>() {
                         public URL[] run() {
-                            File[] classPath = getClassPath(bootClassPath);
+                            // Skip empty path in boot class path i.e. not default to use CWD
+                            File[] classPath = getClassPath(bootClassPath, false);
                             int len = classPath.length;
                             Set<File> seenDirs = new HashSet<File>();
                             for (int i = 0; i < len; i++) {
@@ -405,7 +406,7 @@
         return urls;
     }
 
-    private static File[] getClassPath(String cp) {
+    private static File[] getClassPath(String cp, boolean defaultToCwd) {
         File[] path;
         if (cp != null) {
             int count = 0, maxCount = 1;
@@ -419,9 +420,9 @@
             lastPos = pos = 0;
             // Now scan for each path component
             while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
-                if (pos - lastPos > 0) {
+                if (pos > lastPos) {
                     path[count++] = new File(cp.substring(lastPos, pos));
-                } else {
+                } else if (defaultToCwd) {
                     // empty path component translates to "."
                     path[count++] = new File(".");
                 }
@@ -430,7 +431,7 @@
             // Make sure we include the last path component
             if (lastPos < cp.length()) {
                 path[count++] = new File(cp.substring(lastPos));
-            } else {
+            } else if (defaultToCwd) {
                 path[count++] = new File(".");
             }
             // Trim array to correct size
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/GetResource.java	Mon Jan 27 16:22:08 2014 -0800
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014, 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.
+ */
+
+import java.net.URL;
+
+public class GetResource {
+    private static final String RESOURCE_NAME = "test.properties";
+    public static void main(String[] args) {
+        String expect = args[0] + "/" + RESOURCE_NAME;
+        URL url = GetResource.class.getResource(RESOURCE_NAME);
+        System.out.println("getResource found: " + url);
+        if (!url.toString().endsWith(expect)) {
+            throw new RuntimeException(url + " != expected resource " + expect);
+        }
+
+        url = ClassLoader.getSystemResource(RESOURCE_NAME);
+        System.out.println("getSystemResource found: " + url);
+        if (!url.toString().endsWith(expect)) {
+            throw new RuntimeException(url + " != expected resource " + expect);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/GetResource.sh	Mon Jan 27 16:22:08 2014 -0800
@@ -0,0 +1,124 @@
+#
+# Copyright (c) 2014, 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.
+#
+
+# @test
+# @bug 6760902
+# @summary Empty path on bootclasspath is not default to current working
+#          directory for both class lookup and resource lookup whereas
+#          empty path on classpath is default to current working directory.
+#
+# @run shell GetResource.sh
+
+if [ "${TESTSRC}" = "" ] ; then
+    TESTSRC=`pwd`
+fi
+if [ "${TESTCLASSES}" = "" ] ; then
+    TESTCLASSES=`pwd`
+fi
+
+if [ "${TESTJAVA}" = "" ] ; then
+    echo "TESTJAVA not set.  Test cannot execute."
+    echo "FAILED!!!"
+    exit 1
+fi
+
+if [ "${COMPILEJAVA}" = "" ] ; then
+    COMPILEJAVA="${TESTJAVA}"
+fi
+
+# set platform-specific variables
+OS=`uname -s`
+case "$OS" in
+  Windows*)
+    PS=";"
+    ;;
+  CYGWIN* )
+    PS=";"
+    TESTCLASSES=`/usr/bin/cygpath -a -s -m ${TESTCLASSES}`
+    ;;
+  * )
+    PS=":"
+    ;;
+esac
+
+echo TESTSRC=${TESTSRC}
+echo TESTCLASSES=${TESTCLASSES}
+echo TESTJAVA=${TESTJAVA}
+echo ""
+
+${COMPILEJAVA}/bin/javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} \
+        -d ${TESTCLASSES} \
+        ${TESTSRC}/GetResource.java  || exit 10
+
+setup() {
+    dest=${TESTCLASSES}/$1
+    rm -rf $dest
+    mkdir $dest
+    cp ${TESTSRC}/test.properties $dest
+    cp ${TESTCLASSES}/GetResource.class $dest
+}
+
+
+count=0
+runTest() {
+    expected=$1;
+    vmoption=$2; shift; shift
+    count=`expr $count+1`
+    echo "Test $count : $vmoption $@"
+    ${TESTJAVA}/bin/java ${TESTVMOPTS} "$vmoption" $@ \
+        GetResource $expected     || exit $count
+}
+
+# run test
+setup "a"
+setup "b"
+
+cd ${TESTCLASSES}
+DIR=`pwd`
+
+#    Expected    -Xbootclasspath
+#    Location    or -classpath
+runTest "a"      "-Xbootclasspath/p:a"
+runTest "a"      "-Xbootclasspath/p:a${PS}b"
+runTest "b"      "-Xbootclasspath/p:b" 
+runTest "b"      "-Xbootclasspath/p:b${PS}a"
+runTest "a"      -cp a
+runTest "a"      -cp "a${PS}b"
+runTest "b"      -cp b
+runTest "b"      -cp "b${PS}a"
+
+cd ${DIR}/a
+
+runTest "a"      "-Xbootclasspath/p:."
+runTest "b"      "-Xbootclasspath/p:../b" 
+
+# no -classpath
+runTest "a"      -cp "${PS}"                            
+runTest "b"      -cp "../b"                   
+
+# Test empty path in bootclasspath not default to current working directory
+runTest "b"      "-Xbootclasspath/p:${PS}../b" 
+
+# Test empty path in classpath default to current working directory
+runTest "a"      -cp "${PS}../b"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/test.properties	Mon Jan 27 16:22:08 2014 -0800
@@ -0,0 +1,1 @@
+# empty resource