Java examples for Reflection:Class
get Class Directory
//package com.java2s; import java.io.File; import java.net.URL; public class Main { public static void main(String[] argv) throws Exception { Class testClass = String.class; System.out.println(getClassDirectory(testClass)); }//from w w w . j av a2s . c o m public static String getClassDirectory(Class testClass) { String resource = testClass.getSimpleName() + ".class"; URL ressourceURL = testClass.getResource(resource); String absolutPath = ressourceURL.getPath(); absolutPath = absolutPath.replace('/', File.separatorChar); absolutPath = absolutPath.replace('\\', File.separatorChar); String className = testClass.getName(); String classNamePath = className.replace('.', File.separatorChar); int index = absolutPath.indexOf(classNamePath); String classDir = absolutPath.substring(0, index); return classDir; } }