Friday, 12 February 2016

  • Get current running class dir path
  • String jarOrBuildClassDir = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
    

    It will return 'build/classes/main' if you run it on IDE

    /Users/demo/workspace/MyDemo/build/classes/main/
    

    It will return the location of 'jar' file which the class archived if you run it in a jar file

    /Users/demo/workspace/MyDemo/install/MyDemo/lib/MyDemo.jar
    
  • Get special resources file path that copy from 'src/main/java/resources/com/demo/test.xsl'
  • URL xslFileURL = getClass().getResource("/com/demo/test.xsl")
    

    It will return 'build/resources/main' if you run it on IDE

    /Users/demo/workspace/MyDemo/build/resources/main/com/demo/test.xsl
    

    It will return the location of 'jar' file which the class archived if you run it in a jar file

    file:/Users/demo/workspace/MyDemo/install/MyDemo/lib/MyDemo.jar!/com/demo/test.xsl
    

No comments:

Post a Comment