View Javadoc
1   package org.codehaus.mojo.animal_sniffer.jbcpd;
2   /*
3    * The MIT License
4    *
5    * Copyright (c) 2009 codehaus.org.
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   *
25   */
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Enumeration;
30  
31  public final class ShowClassPath {
32  
33      public static void main(String[] args) {
34          String cp = System.getProperty("sun.boot.class.path");
35          if (cp != null) {
36              System.out.println(cp);
37              return;
38          }
39          cp = System.getProperty("java.boot.class.path");
40          if (cp != null) {
41              System.out.println(cp);
42              return;
43          }
44          Enumeration i = System.getProperties().propertyNames();
45          String name = null;
46          while (i.hasMoreElements()) {
47              String temp = (String) i.nextElement();
48              if (temp.indexOf(".boot.class.path") != -1) {
49                  if (name == null) {
50                      name = temp;
51                  } else {
52                      System.err.println("Cannot auto-detect boot class path " + System.getProperty("java.version"));
53                      System.exit(1);
54                  }
55              }
56          }
57          if (name == null) {
58              String version = System.getProperty("java.version");
59              if (version.startsWith("1.1.")) {
60                  // by default, the current directory is added to the classpath
61                  // we therefore need to strip that out
62                  cp = System.getProperty("java.class.path");
63                  cp = removeAll(cp, ".");
64                  cp = removeAll(cp, new File(".").getAbsolutePath());
65                  try {
66                      cp = removeAll(cp, new File(".").getCanonicalPath());
67                  } catch (IOException e) {
68                      // ignore
69                  }
70                  cp = removeAll(cp, new File(".").getAbsolutePath() + System.getProperty("file.separator"));
71                  try {
72                      cp = removeAll(cp, new File(".").getCanonicalPath() + System.getProperty("file.separator"));
73                  } catch (IOException e) {
74                      // ignore
75                  }
76                  System.out.println(cp);
77                  return;
78              }
79              System.err.println("Cannot auto-detect boot class path " + System.getProperty("java.version") + " " +
80                      System.getProperty("java.class.path"));
81              System.exit(1);
82          }
83          System.out.println(System.getProperty(name));
84      }
85  
86      private static boolean isWindows() {
87          return System.getProperty("os.name").toUpperCase().startsWith("WINDOWS");
88      }
89  
90      private static String removeAll(String cp, String prefix) {
91          String pathSeparator = System.getProperty("path.separator");
92          if (cp.startsWith(prefix + pathSeparator)) {
93              cp = cp.substring(prefix.length() + pathSeparator.length());
94          }
95          int j;
96          while (-1 != (j = cp.indexOf(pathSeparator + prefix + pathSeparator))) {
97              cp = cp.substring(0, j) + cp.substring(j + prefix.length() + pathSeparator.length());
98          }
99          if (cp.endsWith(pathSeparator + prefix)) {
100             cp = cp.substring(0, cp.length() - prefix.length() + pathSeparator.length());
101         }
102         if (isWindows()) {
103             // we might have the prefix or the classpath case differing
104             if (cp.toUpperCase().startsWith((prefix + pathSeparator).toUpperCase())) {
105                 cp = cp.substring(prefix.length() + pathSeparator.length());
106             }
107             while (-1 != (j = cp.toUpperCase().indexOf((pathSeparator + prefix + pathSeparator).toUpperCase()))) {
108                 cp = cp.substring(0, j) + cp.substring(j + prefix.length() + pathSeparator.length());
109             }
110             if (cp.toUpperCase().endsWith((pathSeparator + prefix).toUpperCase())) {
111                 cp = cp.substring(0, cp.length() - prefix.length() + pathSeparator.length());
112             }
113         }
114         return cp;
115     }
116 }