2013-08-23 147 views
7

我需要从Scala设置环境变量(PATH)。如何从Scala设置环境变量?

我尝试这样做:

val cmd = Seq("export", "PATH='bla'") 
cmd.lines 

但我得到的错误:

java.io.IOException: Cannot run program "export": error=2, No such file or directory 
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) 
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:68) 
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.lines(ProcessBuilderImpl.scala:140) 
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.lines(ProcessBuilderImpl.scala:106) 
at .<init>(<console>:12) 
at .<clinit>(<console>) 
at .<init>(<console>:11) 
at .<clinit>(<console>) 
at $print(<console>) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) 
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914) 
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546) 
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577) 
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543) 
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694) 
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745) 
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651) 
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542) 
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550) 
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822) 
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851) 
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73) 
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:64) 
at sbt.Console.console0$1(Console.scala:23) 
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24) 
at sbt.TrapExit$.executeMain$1(TrapExit.scala:33) 
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42) 
Caused by: java.io.IOException: error=2, No such file or directory 
    at java.lang.UNIXProcess.forkAndExec(Native Method) 
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:135) 
    at java.lang.ProcessImpl.start(ProcessImpl.java:130) 
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) 
    ... 35 more 

有一些其他的方法呢?从文档

+0

您是否试图从属性设置的Scala启动另一个进程,或者你想在Scala程序退出后让属性保留? – joescii

+2

重复的http://stackoverflow.com/q/9443190/1296806,它有一个像下面这样简洁的答案。 –

回答

10

举例sys.process.Process

apply("java", new java.ioFile("/opt/app"), "CLASSPATH" -> "library.jar") 

编辑为更有帮助的措辞:

也就是说,当您生成一个子进程指定ENV。

当前进程的环境是只读的;见System.getenv,或比较抽象sys.propssys.env

事实上,一个shell增加了它在赋予导出变量的subshel​​l上赋予的环境是一个shell约定。见3.7.4在bash的引用,例如:

On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment. The export and ‘declare -x’ commands allow parameters and functions to be added to and deleted from the environment. If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old. The environment inherited by any executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the unset and ‘export -n’ commands, plus any additions via the export and ‘declare -x’ commands.

这是第一次我的回答是长于the Daniel Sobral answer它复制。

2

'export'不是可执行文件,它是一个shell内置命令。如果你试图在父shell中设置路径,那么你不能。您可以将其设置为您执行的新shell。这实际上更像是一个unix FAQ。