Skip to content

Instantly share code, notes, and snippets.

@K0NRAD
Last active August 29, 2015 13:56
Show Gist options
  • Save K0NRAD/8897652 to your computer and use it in GitHub Desktop.
Save K0NRAD/8897652 to your computer and use it in GitHub Desktop.
Where does the JVM a class loaded
public class GetLocationOfClassTest {
@Test
public void getClasspathOfClass() throws Exception {
String locationOfThisClass = getLocationOfClass(this.getClass());
String locationOfJUnit4Class = getLocationOfClass(JUnit4.class);
String expectedLocationOfThisClass = "/Users/<User>/Documents/project.java/studies/target/test-classes/";
String expectedLocationOfJUnit4Class = "/Users/<User>/.m2/repository/junit/junit/4.11/junit-4.11.jar";
assertThat(locationOfThisClass, is(expectedLocationOfThisClass));
assertThat(locationOfJUnit4Class, is(expectedLocationOfJUnit4Class));
}
public String getLocationOfClass(Class aClass) throws URISyntaxException {
URL location = aClass.getProtectionDomain().getCodeSource().getLocation();
return location.toURI().getPath();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment