[자바] 현재 실행 중인 위치가 IDE 인지, 아니면 jar 인지 확인하는 방법

2023. 8. 1. 22:32Java

작성일 : 2020. 9. 14.

[API]
getClass().getResource("").getProtocol();

getClass().getResource("") 를 IDE 에서 실행한 경우,
"file:/C:/..." 처럼 앞에 `file:` 이 붙으며,

getClass().getResource("") 를 jar 에서 실행한 경우,
"jar:/..." 처럼 앞에 `jar:` 이 붙는다.

즉, toString() 으로 변환해서 앞에 붙는 문자열을 if 조건에서 검사하거나
Resource 클래스가 지닌 getProtocol() 메서드로 가져오는 방식이 있다.

getProtocol() 결과 : file 또는 jar 문자열을 반환함.

출처)

 

Can you tell on runtime if you're running java from within a jar?

I have an application that some of my users run from Eclipse, and others run it by using a jar file. I want some actions to be done when running from within the jar, but I don't want them to be done

stackoverflow.com