配置Scala工作表的工作目录 [英] Configure working directory of Scala worksheet

查看:37
本文介绍了配置Scala工作表的工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Scala 工作表(和 Scala 解释器)的工作目录是 Eclipse 项目路径,而不是 Eclipse 安装目录.我如何(以非编程方式)实现这一目标?

我知道我可以使用 System.setProperty("user.dir", "..."),但恕我直言,不属于代码.此外,它似乎不起作用:

object ScratchWS {System.setProperty("user.dir", "C:\\")//>res0: String = C:\adt-bundle-windows-x86_64-20130219\eclipsenew File("putty.exe").exists()//>res1:布尔值 = 假new File("C:\\putty.exe").exists()//>res2:布尔值 = 真}

解决方案

从 Scala Worksheet 0.2.1 开始,无法控制工作表工作目录.

出于安全原因,一旦 JVM 运行,就无法(直接)直接更改 JVM 的工作方式.有关详细信息,请参阅在 Java 中更改当前工作目录?.>

因此,始终指定完全限定的路径或从完全限定的锚点"指定相对路径通常是一种很好的做法.

这是我在 Scala 工作表中获得这样一个锚点"的一个技巧

object WorksheetProjectDirHack {//糟糕.... 见:https://github.com/scala-ide/scala-worksheet/issues/102导入属性._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")}

它基本上通过利用在 Eclipse 项目目录中创建并附加到 Scala Worksheet 类路径的 .worksheet/bin 目录的存在来工作.

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
}

解决方案

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.

这篇关于配置Scala工作表的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆