Question

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
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top