Skip to content

Instantly share code, notes, and snippets.

@jaceshim
Created February 26, 2018 17:00
Show Gist options
  • Save jaceshim/5bfd4c09009e3fcf14179dfbbeec7e32 to your computer and use it in GitHub Desktop.
Save jaceshim/5bfd4c09009e3fcf14179dfbbeec7e32 to your computer and use it in GitHub Desktop.
Find jar path loaded class in spring controller
@Controller
public class ClassLoaderController {
/**
*
* @param clazzName 패키지를 포함한 클래스명 ex) javax.servlet.http.HttpServlet
* @return
*/
@RequestMapping(value = "/find/jar")
@ResponseBody
public String find(@RequestParam String clazzName) {
String result = "";
if (StringUtils.isNoneBlank(clazzName)) {
clazzName = clazzName.replace('.', '/').trim();
clazzName = "/" + clazzName + ".class";
java.net.URL classUrl = this.getClass().getResource(clazzName);
if (classUrl != null) {
String jarFile = classUrl.getFile();
int indexNo = jarFile.indexOf("!");
if (indexNo != -1) {
result = jarFile.substring(0, indexNo);
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment