scala.sys.process.Process抛出RuntimeException

时间:2014-02-17 14:27:20

标签: scala

我试图以递归方式获取两个目录的差异并将它们注册为github问题。

如果目录中有几个文件,scala.sys.process.Process就像我期望的那样工作。

➜  Desktop  ls -la 
(snip)
drwxr-xr-x@  5 garbagetown  staff   170 Sep 19 22:53 2.1.5
drwxr-xr-x@  5 garbagetown  staff   170 Sep 19 11:26 2.2.0
(snip)
➜  Desktop  ls -la 2.1.5/style 
(snip)
-rw-r--r--@ 1 garbagetown  staff   1206 Sep 19 22:53 book.css
-rw-r--r--@ 1 garbagetown  staff    278 Sep 19 22:53 external.png
-rw-r--r--@ 1 garbagetown  staff    175 Sep 19 22:53 header-pattern.png
-rw-r--r--@ 1 garbagetown  staff  12985 Sep 19 22:53 main.css

➜  Desktop  scala
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.sys.process.Process
import scala.sys.process.Process

scala> Process("diff 2.1.5/style 2.2.0/style").!!
res0: String = ""

但是这些目录有很多文件和目录,scala.sys.process.Process会抛出RuntimeException,如下所示。

➜  Desktop  ls -la 2.1.5/manual 
(snip)
-rw-r--r--@  1 garbagetown  staff   9139 Sep 19 22:53 Highlights.md
-rw-r--r--@  1 garbagetown  staff   2737 Sep 19 22:53 Home.md
-rw-r--r--@  1 garbagetown  staff   8444 Sep 19 22:53 Migration.md
-rw-r--r--@  1 garbagetown  staff  15202 Sep 19 22:53 Modules.md
-rw-r--r--@  1 garbagetown  staff    670 Sep 19 22:53 User-Groups-around-the-World.md
-rw-r--r--@  1 garbagetown  staff    937 Sep 19 22:53 _Sidebar.md
drwxr-xr-x@  3 garbagetown  staff    102 Sep 19 22:53 about
drwxr-xr-x@  7 garbagetown  staff    238 Sep 19 22:53 book
drwxr-xr-x@  8 garbagetown  staff    272 Sep 19 22:53 detailledTopics
drwxr-xr-x@  9 garbagetown  staff    306 Sep 19 22:53 gettingStarted
drwxr-xr-x@  9 garbagetown  staff    306 Sep 19 22:53 hacking
drwxr-xr-x@  6 garbagetown  staff    204 Sep 19 22:53 javaGuide
drwxr-xr-x@ 16 garbagetown  staff    544 Sep 19 22:53 sandbox
drwxr-xr-x@  7 garbagetown  staff    238 Sep 19 22:53 scalaGuide

➜  Desktop  scala 
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.sys.process.Process
import scala.sys.process.Process

scala> Process("diff 2.1.5/manual 2.2.0/manual").!!
java.lang.RuntimeException: Nonzero exit value: 1
    at scala.sys.package$.error(package.scala:27)
    at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:131)
    at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:101)
    at .<init>(<console>:9)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    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:601)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

diff有一种略微非传统的方式来传达退出状态。通常POSIX utils在成功时返回0,在出错时返回非零。如果没有找到差异,则Diff返回0,如果找到差异,则返回1,等等。Process认为执行时出错并抛出异常。所以你需要解决这个问题。

一种方法是改为使用此方法(doc):

 abstract def lines_!(log: ProcessLogger): Stream[String]

它不会抛出非零错误状态。

例如:

Process("diff 2.1.5/manual 2.2.0/manual").lines_!.foreach(println)