关于CLASSLOADER的一切- -| 回首页 | 2005年索引 | - -《罪恶之城》:绝望的黑色诗句

JWhcih的源代码- -

                                      

package com.flowerknight.classloader;

/*
 * Copyright (C) 2001 Clarkware Consulting, Inc.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  3. Neither the name of Clarkware Consulting, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without prior written permission. For written
 *     permission, please contact clarkware@clarkware.com.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * CLARKWARE CONSULTING OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN  ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.File;
import java.net.URL;
import java.util.StringTokenizer;

/**
 * <code>JWhich</code> is a utility that takes a Java class name
 * and displays the absolute pathname of the class file that would
 * be loaded first by the class loader, as prescribed by the
 * class path.
 * <code>JWhich</code> 是一个工具类,可以获得java类的文件名和一个绝对路径,
 * 这个绝对路径是classloader在装载该类时获得的第一个绝对路径。
 * (CLASSLOADER将使用第一个发现指定类的绝对路径来装载该类)
 *
 * <p>
 * <code>JWhich</code> also validates the class path and reports
 * any non-existent or invalid class path entries.
 * <code>JWhich</code> 也可以用来验证类路经和报告不存在的或无效的类路经。
 *
 * <p>
 * Usage is similar to the UNIX <code>which</code> command.
 * JWhich的灵感来自UNIX系统中的which命令,它的Usage也是类似which命令的。
 * <p>
 * Example uses:
 * <p>
 * <blockquote>
 *
 * 寻找<code>java.util.ArrayList.class</code>的引用路径
 * <pre>java JWhich java.util.ArrayList</pre>
 *
 * 找到java.util.ArrayList的第一条引用路径(绝对路径)
 * <pre>Class 'java.util.ArrayList' found in
 *     'file:/C:/Java/jdk1.5.0_04/jre/lib/rt.jar!/java/util/ArrayList.class'</pre>
 *
 * 系统CLASSPATH上的所有类库归档文件,上面找到的绝对路径就来自其中。
 * <pre>
 * Classpath:
 * .
 * C:\Java\jdk1.5.0_04\lib\tools.jar
 * C:\Java\jdk1.5.0_04\lib\dt.jar
 * C:\Java\jdk1.5.0_04\jre\lib\rt.jar
 * D:\Oracle\Ora81\orb\classes\yoj.jar
 * D:\Oracle\Ora81\orb\classes\share.zip
 * </pre>
 * </blockquote>
 *
 * @author <a href="Mike">mailto:mike@clarkware.com">Mike Clark</a>
 * @author <a href="Clarkware">http://www.clarkware.com">Clarkware Consulting, Inc.</a>
 * @author <a href="Kevin">http://www.flowerknight.bokee.com/">Kevin Lee </a>
 */

public class JWhich {

 private static String CLASSPATH;

 /**
  * Prints the absolute pathname of the class file
  * containing the specified class name, as prescribed
  * by the class path.
  *
  * @param className Name of the class.
  */
 public static void which(String className) {

  URL classUrl = findClass(className);

  if (classUrl == null) {
   System.out.println("\nClass '" + className + "' not found.");
  } else {
   System.out.println(
    "\nClass '" + className + "' found in \n'" + classUrl.getFile() + "'");
  }

  validate();

  printClasspath();
 }

 /**
  * Returns the URL of the resource denoted by the specified
  * class name, as prescribed by the class path.
  *
  * @param className Name of the class.
  * @return Class URL, or null of the class was not found.
  */
 public static URL findClass(final String className) {
  return JWhich.class.getResource(asResourceName(className));
 }

 protected static String asResourceName(String resource) {
  if (!resource.startsWith("/")) {
   resource = "/" + resource;
  }
  resource = resource.replace('.', '/');
  resource = resource + ".class";
  return resource;
 }

 /**
  * Validates the class path and reports any non-existent
  * or invalid class path entries.
  * <p>
  * Valid class path entries include directories, <code>.zip</code>
  * files, and <code>.jar</code> files.
  */
 public static void validate() {

  StringTokenizer tokenizer = new StringTokenizer(getClasspath(), File.pathSeparator);

  while (tokenizer.hasMoreTokens()) {
   String element = tokenizer.nextToken();
   File f = new File(element);

   if (!f.exists()) {
    System.out.println("\nClasspath element '" + element + "' " + "does not exist.");
   } else if (
    (!f.isDirectory())
     && (!element.toLowerCase().endsWith(".jar"))
     && (!element.toLowerCase().endsWith(".zip"))) {

    System.out.println(
     "\nClasspath element '"
      + element
      + "' "
      + "is not a directory, .jar file, or .zip file.");

   }
  }
 }

 public static void printClasspath() {

  System.out.println("\nClasspath:");
  StringTokenizer tokenizer = new StringTokenizer(getClasspath(), File.pathSeparator);
  while (tokenizer.hasMoreTokens()) {
   System.out.println(tokenizer.nextToken());
  }
 }

 public static void setClasspath(String classpath) {
  CLASSPATH = classpath;
 }

 protected static String getClasspath() {
  if (CLASSPATH == null) {
   setClasspath(System.getProperty("java.class.path"));
  }

  return CLASSPATH;
 }

 private static void instanceMain(String[] args) {

  if (args.length == 0) {
   printUsage();
  }

  for (int cmdIndex = 0; cmdIndex < args.length; cmdIndex++) {

   String cmd = args[cmdIndex];

   if ("-help".equals(cmd)) {
    printUsage();
   } else {
    which(cmd);
   }
  }
 }

 private static void printUsage() {

  System.out.println("\nSyntax: java JWhich [options] className");
  System.out.println("");
  System.out.println("where options include:");
  System.out.println("");
  System.out.println("\t-help     Prints usage information.");
  System.out.println("");
  System.out.println("Examples:");
  System.out.println("\tjava JWhich MyClass");
  System.out.println("\tjava JWhich my.package.MyClass");
  System.exit(0);
 }

 public static void main(String args[]) {
  JWhich.instanceMain(args);
 }
}

- 作者: 橘子 访问统计: 2005年08月5日, 星期五 13:04 加入博采

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=2492302

博客手拉手

[2005-08-05]    《我的海洋》

[2005-08-05]    钱颖一:经济学科在美国

[2005-08-05]    色情漫画泛滥王菲爱女童童,风情伊人时尚图库星座运程极品动漫

[2005-08-05]    老虎的艺术

[2005-08-05]    感谢有你!

回复

评论内容: