JavaDemo——获得linux运行的jar包位置和文件

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运行结果:

获得jar位置参考:https://www.cnblogs.com/keyi/p/6282838.html


版权声明:本文为FlyLikeButterfly原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。