Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. scala /path/to/file/file.scala
  2.  
  3. scala ./to./file/file.scala
  4.  
  5. import java.io.File
  6.  
  7. printf("[%s]n",new File(getScriptName).getCanonicalPath)
  8. System.exit(0)
  9.  
  10. def getScriptName:String = {
  11. // this approach works on the sun jvm:
  12. val jcmd = System.getProperty("sun.java.command","")
  13. jcmd.split("""s+""").toList.reverse.head match {
  14. case name if new File(name).exists => name
  15. case _ =>
  16. // for this to work, you need to set -Dscala.script.name at script launch
  17. System.getProperty("scala.script.name","") match {
  18. case name if new File(name).exists => name
  19. case _ =>
  20. // last resort: derive script basename from stackdump
  21. val basename = {
  22. val filenames = new RuntimeException("").getStackTrace.map { it
  23. => it.getFileName
  24. }
  25. filenames.indexOf("NativeMethodAccessorImpl.java") match {
  26. case -1 => filenames.reverse.head
  27. case num => filenames(num - 1)
  28. }
  29. }
  30. // then search the system PATH for the script file
  31. import scala.collection.JavaConversions._
  32. val sep = System.getProperty("path.separator")
  33. val path = System.getenv("PATH").split(sep)
  34. path.map(new File(_,basename)).find( _.exists ) match {
  35. case Some(scriptfile) => scriptfile.getCanonicalPath
  36. case _ => basename // better than nothing
  37. }
  38. }
  39. }
  40. }
  41.  
  42. #!/usr/bin/env sh
  43. for SCRIPT_NAME; do true ; done # set SCRIPT_NAME to the last argument
  44. JAVA_OPTS="$JAVA_OPTS -Dscala.script.name=${SCRIPT_NAME}"
  45. $SCALA_HOME/bin/scala "$@"
  46.  
  47. #!/usr/bin/env scala
  48. !#
  49. printf("%sn",System.getProperty("scala.script.name"))
  50.  
  51. #!/bin/sh
  52. export SCRIPT_PATH=$(readlink -f "$0") && exec scala "$0" "$@"
  53. !#
  54.  
  55. val scriptPath = System.getenv("SCRIPT_PATH")
  56.  
  57. import java.net.URL
  58.  
  59. object LocationTest extends App {
  60. val classDirURL = LocationTest.getClass.getResource("LocationTest.class")
  61. val classDirPath = classDirURL.getPath.replaceAll("%20", " ")
  62. println(classDirPath)
  63. }
  64.  
  65. /Users/connor/desktop/LocationTest.class
  66.  
  67. scala -DPARAM_DIR=tmp Foo
  68.  
  69. object Foo extends App {
  70. val dir = System.getProperty ("PARAM_DIR")
  71. println ("Dir: " + dir)
  72. }
  73.  
  74. /path/to/file/file.scala
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement