Java ClassPath Get getClassPath(Object obj, boolean bOnlyPath)

Here you can find the source of getClassPath(Object obj, boolean bOnlyPath)

Description

get Class Path

License

Open Source License

Declaration

public static String getClassPath(Object obj, boolean bOnlyPath) 

Method Source Code

//package com.java2s;
/*//from  w w w. j a va  2 s  .  c om
 *  JClassPathHelper.java
 *
 *  Created on 5. Mai 2005, 21:15
 *
 *  Copyright (C) 5. Mai 2005  <Reiner>

 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

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

public class Main {
    public static String getClassPath(Object obj, boolean bOnlyPath) {
        String path = null;
        if (obj != null) {
            String className = obj.getClass().getName();
            int index = className.lastIndexOf(".");
            if (index >= 0)
                className = className.substring(index + 1,
                        className.length());
            URL url = obj.getClass().getResource(className + ".class");
            if (url != null) {
                try {
                    String str = url.toString();
                    int endJar = str.indexOf("!/");
                    if (endJar >= 0) {
                        String prefix = "jar:";
                        url = new URL(
                                str.substring(prefix.length(), endJar));
                    }
                    File file = new File(url.toURI());
                    str = file.getAbsolutePath();
                    if (bOnlyPath) {
                        if ((index = str.lastIndexOf(File.separatorChar)) >= 0)
                            path = str.substring(0, index);
                        else
                            path = str; // ???
                    } else
                        path = str;
                } catch (Exception ex) {
                }
            }
        }
        return path;
    }
}

Related

  1. getClassPath(Class clazz)
  2. getClasspath(ClassLoader classLoader)
  3. getClassPath(ClassLoader loader)
  4. getClasspath(ClassLoader loader)
  5. getClassPath(File dir)
  6. getClassPath(String packageName, String name)
  7. getClassPathAsString(ClassLoader classLoader)
  8. getClasspathDir(Class klass)
  9. getClasspathFile(Class clz)