Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure working directory of Scala worksheet

I would like the working directory for Scala worksheet (and the Scala interpreter) to be the Eclipse project path rather than the Eclipse installation directory. How can I (non programmatically) achieve that?

I know that I can use System.setProperty("user.dir", "..."), but IMHO that does not belong in the code. Further, it does not seem to work:

object ScratchWS {
  System.setProperty("user.dir", "C:\\")          //> res0: String = C:\adt-bundle-windows-x86_64-20130219\eclipse
  new File("putty.exe").exists()                  //> res1: Boolean = false

  new File("C:\\putty.exe").exists()              //> res2: Boolean = true
}
like image 221
gzm0 Avatar asked May 08 '13 18:05

gzm0


People also ask

What is worksheet in Scala?

A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. Worksheets are like a REPL session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, etc.


1 Answers

As of Scala Worksheet 0.2.1 it is not possible to control the worksheet working directory.

For security reasons, once a JVM is running, it is not (directly) possible to change the JVM's working directly. See Changing the current working directory in Java? for details.

Therefore, it is generally good practice to always specify fully qualified paths, or specify relative paths from a fully qualified "anchor point".

Here's a hack I came up with for getting such an "anchor point" in the Scala Worksheet

object WorksheetProjectDirHack {
    // Yuck.... See: https://github.com/scala-ide/scala-worksheet/issues/102
    import Properties._
    val pathSep = propOrElse("path.separator", ":")
    val fileSep = propOrElse("file.separator", "/")
    val projectDir = javaClassPath.split(pathSep).
        filter(_.matches(".*worksheet.bin$")).head.
        split(fileSep).dropRight(2).mkString(fileSep)

    val otherProjectFile = new File(projectDir, "src/main/resources/data.bin")
}

It basically works by taking advantage of the the existence of the .worksheet/bin directory created in your Eclipse project directory and appended to the Scala Worksheet classpath.

like image 67
metasim Avatar answered Oct 09 '22 19:10

metasim