Configure working directory of Scala worksheet

旧时模样 提交于 2019-12-22 04:14:08

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/16447460/configure-working-directory-of-scala-worksheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!