【问题标题】:How to get the name of the current script?如何获取当前脚本的名称?
【发布时间】:2017-09-09 05:03:21
【问题描述】:

如何获取当前脚本的名称?

例如,如果我运行myscript.sc,我想知道myscript 的值 我需要它来杀死仍在运行的脚本的先前实例。

【问题讨论】:

  • 执行this.getClass.getProtectionDomain.getCodeSource.getLocation.getPath 命令会在Linux 系统中提供/tmp 文件夹上的路径。也许这是最接近实际文件名的方法。 This comment 给了我改变你正在运行的脚本的 tempdir 属性的想法。该目录中的任何文件都可能成为正在运行的 scala 脚本的候选对象。
  • 我想到的另一个解决方案是使用scala -Xprint:parser filename.sc * 2> tempFile 执行脚本。 tempFile 的第一行将始终包含以下形式的一行:[[syntax trees at end of parser]] // filename.sc: tree ...,然后可用于提取文件名!
  • 如果我用这个内容写一个脚本:println(this.getClass.getProtectionDomain.getCodeSource.getLocation().getPath),那么我得到null作为结果。
  • 我不知道如何将-Xprint:parser 参数传递给amm 二进制文件。

标签: scala ammonite


【解决方案1】:

sourcecode.File() 将返回当前脚本的名称

【讨论】:

  • 请举例说明。
猜你喜欢
  • 1970-01-01
  • 2011-05-08
  • 2018-01-14
  • 2011-10-15
  • 2016-11-02
  • 2011-05-12
  • 2022-06-16
  • 2020-04-02
  • 2014-03-06
相关资源
最近更新 更多