Last active
August 29, 2015 13:56
-
-
Save K0NRAD/8897652 to your computer and use it in GitHub Desktop.
Where does the JVM a class loaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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