可以使用以下代码来获取src目录下所有的包名,类名,方法名 以及通过一个类名获得该类下的所有方法名。
import javaioFile;
import javalangreflectMethod;
public class LoopApp {
public static void main(String[] args) throws Exception {
String packageName = "";
File root = new File(SystemgetProperty("userdir") + "\\src");
loop(root, packageName);
}
public static void loop(File folder, String packageName) throws Exception {
File[] files = folderlistFiles();
for (int fileIndex = 0; fileIndex < fileslength; fileIndex++) {
File file = files[fileIndex];
if (fileisDirectory()) {
loop(file, packageName + filegetName() + "");
} else {
listMethodNames(filegetName(), packageName);
}
}
}
public static void listMethodNames(String filename, String packageName) {
try {
String name = filenamesubstring(0, filenamelength() - 5);
Object obj = ClassforName(packageName + name);
Method[] methods = objgetClass()getDeclaredMethods();
Systemoutprintln(filename);
for (int i = 0; i < methodslength; i++) {
Systemoutprintln("\t" + methods[i]getName());
}
} catch (Exception e) {
Systemoutprintln("exception = " + egetLocalizedMessage());
}
}
}
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:requestgetRequestURI()
结果:/TEST/testjsp
(2)得到工程名:requestgetContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:requestgetServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/testjsp
(4)得到页面所在服务器的全路径:applicationgetRealPath("页面jsp")
结果:D:/resin/webapps/TEST/testjsp
(5)得到页面所在服务器的绝对路径:absPath=new javaioFile(applicationgetRealPath(requestgetRequestURI()))getParent();
结果:D:/resin/webapps/TEST
2在类中取得路径:
(1)类的绝对路径:String u=ClassclassgetClass()getResource("/")getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:SystemgetProperty("userdir")
结果:D:/TEST
3在Servlet中取得路径:
(1)得到工程目录:requestgetSession()getServletContext()getRealPath("") 参数可具体到包名。
结果:E:/Tomcat/webapps/TEST
(2)得到IE地址栏地址:requestgetRequestURL()
结果:>
使用下面这个PathUtil的getProgramPath()就可以获得当前程序运行的目录。
import javanetURL;
import javanetURLDecoder;
class PathUtil {
/
Get the env of windir, such as "C:\WINDOWS"
@return the env of windir value
/
public static String getWindir() {
return Systemgetenv("windir");
}
/
Get file separator, such as "/" on unix
@return the separator of file
/
public static String getFileSeparator() {
return SystemgetProperty("fileseparator");
}
/
Get line separator, such as "\n" on unix
@return the separator of line
/
public static String getLineSeparator() {
return SystemgetProperty("lineseparator");
}
/
Get programPath
@return programPath
/
public static String getProgramPath() {
Class<PathUtil> cls = PathUtilclass;
ClassLoader loader = clsgetClassLoader();
//
// Get the full name of the class
//
String clsName = clsgetName() + "class";
//
// Get the package that include the class
//
Package pack = clsgetPackage();
String path = "";
//
// Transform package name to path
//
if (pack != null) {
String packName = packgetName();
//
// Get the class's file name
//
clsName = clsNamesubstring(packNamelength() + 1);
//
// If package is simple transform package name to path directly,
// else transform package name to path by package name's
// constituent
//
path = packName;
if (pathindexOf("") > 0) {
path = pathreplace("", "/");
}
path = path + "/";
}
URL url = loadergetResource(path + clsName);
//
// Get path information form the instance of URL
//
String retPath = urlgetPath();
//
// Delete protocol name "file:" form path information
//
try {
int pos = retPathindexOf("file:");
if (pos > -1) {
retPath = retPathsubstring(pos + 5);
}
//
// Delete the information of class file from the information of
// path
//
pos = retPathindexOf(path + clsName);
retPath = retPathsubstring(0, pos - 1);
//
// If the class file was packageed into JAR eg file, delete the
// file name of the corresponding JAR eg
//
if (retPathendsWith("!")) {
retPath = retPathsubstring(0, retPathlastIndexOf("/"));
}
retPath = URLDecoderdecode(retPath, "utf-8");
} catch (Exception e) {
retPath = null;
eprintStackTrace();
}
return retPath;
}
}
测试类:
public class Test{
public static void main(String args[]){
String s = PathUtilgetProgramPath();
Systemoutprintln(s);
}
}
以上就是关于java怎么获取src目录下所有的包名,类名,方法名 以及通过一个类名获得该类下的所有方法名全部的内容,包括:java怎么获取src目录下所有的包名,类名,方法名 以及通过一个类名获得该类下的所有方法名、java 怎么获取web根目录、Java获取程序运行的当前工作目录等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)