JavaDemo——获得linux运行的jar包位置和文件
程序员文章站
2022-06-12 09:37:37
...
Demo:
/**
* createtime : 2018年9月6日 下午5:52:29
*/
package com.useLinuxCmd;
import java.io.File;
import java.util.Date;
/**
* TODO
* @author XWF
*/
public class TestJarPathAndFile {
/**
* @param args
*/
public static void main(String[] args) {
String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
path = path.substring(5,path.length());
System.out.println("jar包目录:"+path);
String folderName = "myfolder";
String filePath = path+folderName;
File file = new File(filePath);
if(file.exists()&&file.isDirectory()) {
String fileAbsolutePath = file.getAbsolutePath();//文件绝对路径
System.out.println("找到了jar包同目录的文件夹:"+fileAbsolutePath);
File[] childFiles = file.listFiles();
for(File childFile:childFiles) {
System.out.println("-childName:"+childFile.getName()+" 最后修改时间:"+new Date(childFile.lastModified()));
}
}else {
System.out.println("文件夹在jar包同目录下不存在,或者不是文件夹");
}
}
}
生成jar包在linux运行结果: