开发者

Get scala-library.jar location

开发者 https://www.devze.com 2023-04-08 16:30 出处:网络
How can I get the full path of scala-library.jar from file system with Scala code? I\'ve tried ways like scala.Console.getClass.getProtectionDomain.getCodeSource.getLocation.getPath but it seemed don\

How can I get the full path of scala-library.jar from file system with Scala code? I've tried ways like scala.Console.getClass.getProtectionDomain.getCodeSource.getLocation.getPath but it seemed don't work (returned 开发者_StackOverflow社区a null pointer).


We use this to adjust the classpath for caliper:

val so = classOf[ScalaObject].getResource("ScalaObject.class").toString
val scalaJar = so.substring(so.lastIndexOf(":") + 1, so.lastIndexOf("!"))

This works in eclipse and a fat-jar from command line under Mac OS and Linux.


One way is to get it through the classpath like this:

System.getProperty("java.class.path").split(java.io.File.pathSeparator)
  .filter{_.contains("scala-library")}.head


A bit similar to the answer by thoredge, but also searches the boot classpath (this should work in Eclipse + ScalaPlugin):

List("java.class.path", "java.boot.class.path", "sun.boot.class.path")
  .flatMap(s => System.getProperty(s, "")
  .split(java.io.File.pathSeparator))
  .find(new java.io.File(_).getName == "scala-library.jar")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号