Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

The stream of consciousness

[JAVA] 특정 폴더 하위 자바 버전 찾기 본문

IT

[JAVA] 특정 폴더 하위 자바 버전 찾기

인터넷실명제 2018. 8. 8. 13:42

컴퓨터에 설치된 자바 목록을 출력해야 할 필요가 생겨서 만들었다.


자바 버전을 확인하기 위해 설정한 root 폴더를 탐색해가며 lib ->rt.jar파일의 MANIFEST.MF파일을 열어 쓰여있는 자바버전을 가져왔다. (다른 방법을 못찾겠다..)


자료형은 Queue구조로 만들었으며, jar파일 작업하는 부분은 

http://nowonbun.tistory.com/321#commentList 블로그에서 긁어왔다.



import java.io.File;
import java.io.FileFilter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class javaversionUpload {
	//파일의 타입이 폴더일때만으로 필터링하기
	private FileFilter fileFilter = new FileFilter() {
		public boolean accept(File dir) {
			return dir.isDirectory();
		}
	};

	public static void main(String[] args) {
		new javaversionUpload().findJavaVersion("C:\\");
	}

	private void findJavaVersion(String path) {
		ArrayList subFiles = new ArrayList();
		File f = new File(path);
		String version = "";
		if (!f.isDirectory()) {
			System.out.println("There is no directory.");
			return;
		}
		//root 파일 추가
		subFiles.add(f);
		while (!subFiles.isEmpty()) {
			System.out.println(((File) subFiles.get(0)).getName());
			//현재 폴더의 이름이 lib이고
			if (((File) subFiles.get(0)).getName().equals("lib")) {
				for (int i = 0; i < ((File) subFiles.get(0)).listFiles().length; i++) {
					File childFile = ((File) subFiles.get(0)).listFiles()[i];
					//자식의 파일 이름 중 rt.jar가 있으면
					if (childFile.getName().equals("rt.jar")) {
						try {
							version = decompress(childFile);
							String target = ((File) subFiles.get(0)).getCanonicalPath();
							System.out.println("Version : " + version);
							System.out.println("Path : " + target);
						} catch (Throwable e) {
							e.printStackTrace();
						}
					}
				}
			}
			try {
				//큐에 폴더들 넣기
				for(int i=0;i<((File) subFiles.get(0)).listFiles(fileFilter).length;i++){
					subFiles.add(((File) subFiles.get(0)).listFiles(fileFilter)[i]);
				}
				//이렇게도 되네요 ^^
				//subFiles.addAll(Arrays.asList(((File) subFiles.get(0)).listFiles(fileFilter)));
			} catch (Exception e) {
				// e.printStackTrace(); //접근 권한이 있는 폴더 예외처리
			}
			//큐의 첫번째 원소 삭제
			subFiles.remove(0);
		}
	}
	//http://nowonbun.tistory.com/321#commentList
	private String decompress(File zipFileName) throws Throwable {
		ZipFile zf = new ZipFile(zipFileName);
		ZipEntry zipentry = null;
		InputStream in = null;
		byte[] buffer = new byte[2048];
		String returnValue = "error", line = "";
		String[] strarr;
		try {
			if ((zipentry = zf.getEntry("META-INF/MANIFEST.MF")) != null) {
				in = zf.getInputStream(zipentry);
				if (in.read(buffer) != -1) {
					line = new String(buffer, 0, buffer.length);
					strarr = line.split("\n");
					for (int i = 0; i < strarr.length; i++) {
						if (strarr[i].startsWith("Implementation-Version")) {
							returnValue = strarr[i].trim();
							String[] tmpValue = returnValue.split(":");
							if (tmpValue.length > 1) {
								returnValue = tmpValue[1].trim();
							}
							break;
						}
					}
				}
			}
		} catch (Throwable e) {
			throw e;
		} finally {
			if (in != null)
				in.close();
			if (zf != null)
				zf.close();
		}
		return returnValue;
	}
}


rt.jar 내부


실행 결과


폴더 검색 범위가 넓으면 시간이 다소 오래걸린다...


'IT' 카테고리의 다른 글

[Jquery]AJAX로 탭메뉴 구현하기  (5) 2018.08.06
Comments