K0NRAD
2/9/2014 - 11:06 AM

Where does the JVM a class loaded

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();
    }
}