Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- scala /path/to/file/file.scala
- scala ./to./file/file.scala
- import java.io.File
- printf("[%s]n",new File(getScriptName).getCanonicalPath)
- System.exit(0)
- def getScriptName:String = {
- // this approach works on the sun jvm:
- val jcmd = System.getProperty("sun.java.command","")
- jcmd.split("""s+""").toList.reverse.head match {
- case name if new File(name).exists => name
- case _ =>
- // for this to work, you need to set -Dscala.script.name at script launch
- System.getProperty("scala.script.name","") match {
- case name if new File(name).exists => name
- case _ =>
- // last resort: derive script basename from stackdump
- val basename = {
- val filenames = new RuntimeException("").getStackTrace.map { it
- => it.getFileName
- }
- filenames.indexOf("NativeMethodAccessorImpl.java") match {
- case -1 => filenames.reverse.head
- case num => filenames(num - 1)
- }
- }
- // then search the system PATH for the script file
- import scala.collection.JavaConversions._
- val sep = System.getProperty("path.separator")
- val path = System.getenv("PATH").split(sep)
- path.map(new File(_,basename)).find( _.exists ) match {
- case Some(scriptfile) => scriptfile.getCanonicalPath
- case _ => basename // better than nothing
- }
- }
- }
- }
- #!/usr/bin/env sh
- for SCRIPT_NAME; do true ; done # set SCRIPT_NAME to the last argument
- JAVA_OPTS="$JAVA_OPTS -Dscala.script.name=${SCRIPT_NAME}"
- $SCALA_HOME/bin/scala "$@"
- #!/usr/bin/env scala
- !#
- printf("%sn",System.getProperty("scala.script.name"))
- #!/bin/sh
- export SCRIPT_PATH=$(readlink -f "$0") && exec scala "$0" "$@"
- !#
- val scriptPath = System.getenv("SCRIPT_PATH")
- import java.net.URL
- object LocationTest extends App {
- val classDirURL = LocationTest.getClass.getResource("LocationTest.class")
- val classDirPath = classDirURL.getPath.replaceAll("%20", " ")
- println(classDirPath)
- }
- /Users/connor/desktop/LocationTest.class
- scala -DPARAM_DIR=tmp Foo
- object Foo extends App {
- val dir = System.getProperty ("PARAM_DIR")
- println ("Dir: " + dir)
- }
- /path/to/file/file.scala
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement