您好我想创建一个程序,在新进程上启动另一个应用程序:
object A{
def main(args: Array[String]) {
Process(????).run
println("new process has been created")
}
}
应该在新进程上运行的应用程序:
object B{
def main(args: Array[String]) {
print("Hello world")
}
}
我知道我可以使用Process(script_string_to_run)运行脚本,但我不知道如何让它运行另一个应用程序..
答案 0 :(得分:0)
B.scala
object B {
def main(args: Array[String]): Unit = println("hello world")
}
现在从另一个Scala程序执行上述程序。
scala> import sys.process._
import sys.process._
scala> "scala B.scala" !!
warning: there was one feature warning; re-run with -feature for details
res1: String =
"hello world
"
请注意,文件的文件名与包含main方法的对象名相同。这种方式scala将识别主类/对象并执行程序的主要方法。如果文件的名称不是包含main方法的对象的名称,那么它将不知道要执行什么。
!
在执行后给出程序的退出状态,而!!
给出程序的输出。
scala> import sys.process._
import sys.process._
scala> val status = "scala B.scala" !
warning: there was one feature warning; re-run with -feature for details
hello world
status: Int = 0
scala> val output = "scala B.scala" !!
warning: there was one feature warning; re-run with -feature for details
output: String =
"hello world
Test.sh
#!/usr/bin/env scala
object B{
def main(args: Array[String]) {
print("Hello world")
}
}
B.main(Array[String]())
现在只需使用
运行B程序"./Test.sh" !!
命令行活动
➜ cd demo
➜ demo ./Test.sh
zsh: permission denied: ./Test.sh
➜ demo chmod +rwx Test.sh
➜ demo ./Test.sh
Hello world%
➜ demo ./Test.sh
Hello world%
➜ demo scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.
scala> import sys.process._
import sys.process._
scala> "./Test.sh" !!
warning: there was one feature warning; re-run with -feature for details
res0: String =
"Hello world
"
scala> :q