2013-08-23 8 views
7

Ich muss Umgebungsvariable (PATH) von Scala setzen.Wie setze ich die Umgebungsvariable von Scala?

Ich versuchte dies:

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

aber ich habe Fehler:

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 

Gibt es eine andere Möglichkeit, das zu tun?

+0

Versuchen Sie, einen anderen Prozess aus Scala mit den festgelegten Eigenschaften zu starten, oder versuchen Sie, die Eigenschaften zu erhalten, nachdem das Scala-Programm beendet wurde? – joescii

+2

Duplikat von http://Stackoverflow.com/q/9443190/1296806, die eine Antwort so kurz wie meine unten hat. –

Antwort

10

Beispiel von doc für sys.process.Process:

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

bearbeiten für weitere hilfreiche Wortschwall:

Das heißt, Sie die env angeben, wenn Sie ein Kind Prozess erzeugen.

Die Umgebung des aktuellen Prozesses ist schreibgeschützt; siehe System.getenv, oder vergleiche die Abstraktionen sys.props und sys.env.

Die Tatsache, dass eine Shell die Umgebung auf Subshells mit exportierten Variablen erweitert, ist eine Shell-Konvention. Siehe 3.7.4 in der Bash-Referenz, zum Beispiel:

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.

Dies ist das erste Mal, meine Antwort war länger als the Daniel Sobral answer es dupliziert.

2

"Export" ist keine ausführbare Datei, es ist ein Shell-Befehl. Wenn Sie versuchen, den Pfad in der übergeordneten Shell festzulegen, können Sie das nicht. Sie können es für eine neue Shell festlegen, die Sie ausführen. Dies ist wirklich eher eine Unix-FAQ.

Verwandte Themen