Java如何获得linux运行的jar包位置呢?
下文笔者讲述在linux中获取jar包位置的方法分享
学习完本篇之后,你将掌握获取jar包位置的方法,如下所示
学习完本篇之后,你将掌握获取jar包位置的方法,如下所示
使用 Thread.currentThread() .getContextClassLoader() .getResource("").toString(); 获取当前类所涉及的路径 然后才能获取jar包运行位置例:获取jar包位置的示例
package com.java265; import java.io.File; import java.util.Date; 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包同目录下不存在,或者不是文件夹"); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。