1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary If a path (directory plus class name) or a directory path exceeds
  27  *          the MAX_PATH on windows, vm should should print an informative
  28  *          warning message in additional to throwing a ClassNotFoundException.
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @run main LongPath
  33  */
  34 
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Arrays;
  38 import jdk.test.lib.compiler.CompilerUtils;
  39 import jdk.test.lib.process.ProcessTools;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 
  42 public class LongPath {
  43 
  44     private static final int MAX_PATH = 260;
  45 
  46     public static void main(String args[]) throws Exception {
  47         Path sourceDir = Paths.get(System.getProperty("test.src"), "test-classes");
  48         Path classDir = Paths.get(System.getProperty("test.classes"));
  49         Path destDir = classDir;
  50 
  51         // create a sub-path so that the destDir length is almost MAX_PATH
  52         // so that the full path (with the class name) will exceed MAX_PATH
  53         int subDirLen = MAX_PATH - classDir.toString().length() - 2;
  54         if (subDirLen > 0) {
  55             char[] chars = new char[subDirLen];
  56             Arrays.fill(chars, 'x');
  57             String subPath = new String(chars);
  58             destDir = Paths.get(System.getProperty("test.classes"), subPath);
  59         }
  60 
  61         CompilerUtils.compile(sourceDir, destDir);
  62 
  63         String bootCP = "-Xbootclasspath/a:" + destDir.toString();
  64         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  65             bootCP, "Hello");
  66 
  67         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  68         try {
  69             output.shouldContain("Hello World");
  70             output.shouldHaveExitValue(0);
  71         } catch (java.lang.RuntimeException e) {
  72             output.shouldContain("java.lang.ClassNotFoundException: Hello");
  73             output.shouldContain("warning: construct full path name failed");
  74             output.shouldHaveExitValue(1);
  75         }
  76 
  77         // increase the length of destDir to slightly over MAX_PATH
  78         destDir = Paths.get(destDir.toString(), "xxxxx");
  79         CompilerUtils.compile(sourceDir, destDir);
  80 
  81         bootCP = "-Xbootclasspath/a:" + destDir.toString();
  82         pb = ProcessTools.createJavaProcessBuilder(
  83             bootCP, "Hello");
  84 
  85         output = new OutputAnalyzer(pb.start());
  86         try {
  87             output.shouldContain("Hello World");
  88             output.shouldHaveExitValue(0);
  89         } catch (java.lang.RuntimeException e) {
  90             output.shouldContain("java.lang.ClassNotFoundException: Hello");
  91             output.shouldContain("os::stat failed");
  92             output.shouldHaveExitValue(1);
  93         }
  94     }
  95 }