2013-08-23 2 views
7

환경 변수 (PATH)를 Scala에서 설정해야합니다. 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 

그렇게 할 수있는 다른 방법이 있나요를? sys.process.Process에 대한 문서에서

+0

속성 집합으로 스칼라에서 다른 프로세스를 시작하려고합니까? 스칼라 프로그램이 종료 된 후에도 속성을 유지하려고합니까? – joescii

+2

http://stackoverflow.com/q/9443190/1296806 중복, 내 답변처럼 간결합니다. –

답변

10

예 : 더 도움이 말씨에 대한

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

편집 : 당신이 자식 프로세스를 생성 할 때

입니다

, 당신은 ENV를 지정합니다.

현재 프로세스의 환경은 읽기 전용입니다. System.getenv을 참조하거나 추상화 sys.propssys.env을 비교하십시오.

내 보낸 변수를 사용하여 하위 셸에서 제공하는 환경을 셸이 강화한다는 사실은 셸 규칙입니다. 예를 들어, bash는 참조 3.7.4를 참조하십시오

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

'내보내기'는 실행 파일이 아니며 쉘 내장 명령입니다. 부모 셸에서 경로를 설정하려는 경우에는 할 수 없습니다. 당신이 실행하는 새로운 쉘을 위해 그것을 설정할 수 있습니다. 이것은 실제로 유닉스 FAQ의 더 많은 것이다.

관련 문제