scala 2.9.1控制檯中的':javap'命令需要'classpath'中的tools.jar(來自JDK6)。從cmd行可以使用'-cp'參數或CLASSPATH環境變量來完成。在sbt項目的類路徑中添加tools.jar
如何爲使用'console'和'console-quick'命令從SBT調用的scala控制檯執行相同操作?
scala 2.9.1控制檯中的':javap'命令需要'classpath'中的tools.jar(來自JDK6)。從cmd行可以使用'-cp'參數或CLASSPATH環境變量來完成。在sbt項目的類路徑中添加tools.jar
如何爲使用'console'和'console-quick'命令從SBT調用的scala控制檯執行相同操作?
很長的答案,這可能會幫助你在別處。
如果我想知道在SBT的東西,我inspect
它:
> inspect console
[info] Task: Unit
[info] Description:
[info] Starts the Scala interpreter with the project classes on the classpath.
[info] Provided by:
[info] {file:/home/dcs/github/anti-xml/}default-39679a/compile:console
[info] Dependencies:
[info] compile:compilers(for console)
[info] compile:full-classpath
[info] compile:scalac-options(for console)
[info] compile:initial-commands(for console)
[info] compile:streams(for console)
[info] Delegates:
[info] compile:console
[info] *:console
[info] {.}/compile:console
[info] {.}/*:console
[info] */compile:console
[info] */*:console
[info] Related:
[info] test:console
好,有一個在compile:full-classpath
一個有趣的依賴。我希望它是compile:full-classpath(for console)
,但事實並非如此。不過,在這種情況下不應該引起麻煩。我們來inspect
吧。
> inspect compile:full-classpath
[info] Task: scala.collection.Seq[sbt.Attributed[java.io.File]]
[info] Description:
[info] The exported classpath, consisting of build products and unmanaged and managed, internal and external dependencies.
[info] Provided by:
[info] {file:/home/dcs/github/anti-xml/}default-39679a/compile:full-classpath
[info] Dependencies:
[info] compile:exported-products
[info] compile:dependency-classpath
[info] Reverse dependencies:
[info] compile:console
[info] Delegates:
[info] compile:full-classpath
[info] *:full-classpath
[info] {.}/compile:full-classpath
[info] {.}/*:full-classpath
[info] */compile:full-classpath
[info] */*:full-classpath
[info] Related:
[info] compile:full-classpath(for doc)
[info] test:full-classpath
[info] test:full-classpath(for doc)
[info] *:full-classpath(for console)
[info] runtime:full-classpath
[info] compile:full-classpath(for console)
好吧,我可以進一步進入依賴關係,但我不認爲這是必要的。讓我們看看裏面有什麼:
> show compile:full-classpath
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist
[info] List(Attributed(/home/dcs/github/anti-xml/target/scala-2.9.1/classes), Attributed(/home/dcs/.sbt/boot/scala-2.9.1/lib/scala-library.jar))
[success] Total time: 0 s, completed Dec 7, 2011 3:49:30 PM
好吧,沒有什麼意外的。我們添加tools.jar
。
要改變我必須使用set
,我必須尊重一堆駱駝案件和其他規則才能使其工作。如果我有compile:full-classpath(for console)
,那會變成fullClasspath in Compile in console
。請注意0中的大寫字母,full-classpath
變成fullClasspath
,以及元素名稱的一般重新排序。 Details here。
我覺得一個人應該能夠採取的show
輸出(或者,至少,inspect
),並給它馬上回set
,但並非如此(就目前而言),所以只是學習這些轉換規則。
我不想重新鍵入所有內容,只需添加一個JAR文件即可。我需要使用+=
。用於改變事物的操作員可以找到here。
類路徑似乎需要一些Attributed
東西。查看SBT維基上的Classpath的詳細文檔,並瞭解如何設計一個。幸運的是,我想要改變的大多數價值觀並不像這一個那樣難以創造。
> set fullClasspath in Compile += Attributed.blank(file("/usr/lib/jvm/java-6-sun-1.6.0.26/lib/tools.jar"))
[info] Reapplying settings...
[info] Set current project to anti-xml (in build file:/home/dcs/github/anti-xml/)
似乎已經工作。讓我們show
其內容確認,因爲即使寫compile
而不是Compile
可能會讓它改變錯誤的東西。
> show compile:full-classpath
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist
[info] List(Attributed(/home/dcs/github/anti-xml/target/scala-2.9.1/classes), Attributed(/home/dcs/.sbt/boot/scala-2.9.1/lib/scala-library.jar), Attributed(/usr/lib/jvm/java-6-sun-1.6.0.26/lib/tools.jar))
[success] Total time: 0 s, completed Dec 7, 2011 3:50:07 PM
是的,它是。讓我們來測試它:
> console
[warn] Credentials file /home/dcs/.ivy2/.credentials does not exist
[info] Starting scala interpreter...
[info]
import com.codecommit.antixml._
bookstore: com.codecommit.antixml.Elem = <bookstore><book><title>For Whom the Bell Tolls</title><author>Hemmingway</author></book><book><title>I, Robot</title><author>Isaac Asimov</author></book><book><title>Programming Scala</title><author>Dean Wampler</author><author>Alex Payne</author></book></bookstore>
books: com.codecommit.antixml.Zipper[com.codecommit.antixml.Elem] = <book><title>For Whom the Bell Tolls</title><author>Hemmingway</author></book><book><title>I, Robot</title><author>Isaac Asimov</author></book><book><title>Programming Scala</title><author>Dean Wampler</author><author>Alex Payne</author></book>
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :javap com.codecommit.antixml.Elem
Compiled from "node.scala"
public class com.codecommit.antixml.Elem extends java.lang.Object implements com.codecommit.antixml.Node,com.codecommit.antixml.Selectable,scala.ScalaObject,scala.Product,scala.Serializable{
public static final scala.Function1 tupled();
public static final scala.Function1 curry();
public static final scala.Function1 curried();
public static final boolean isValidName(java.lang.String);
public scala.collection.Iterator productIterator();
public scala.collection.Iterator productElements();
public java.lang.Object $bslash(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper);
public java.lang.Object $bslash$bslash(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper);
public java.lang.Object $bslash$bslash$bang(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper);
public java.lang.Object select(com.codecommit.antixml.Selector, com.codecommit.antixml.CanBuildFromWithZipper);
public com.codecommit.antixml.Zipper toZipper();
public scala.Option prefix();
public java.lang.String name();
public com.codecommit.antixml.Attributes attrs();
public scala.collection.immutable.Map scope();
public com.codecommit.antixml.Group children();
public com.codecommit.antixml.Elem canonicalize();
public java.lang.String toString();
public com.codecommit.antixml.Group toGroup();
public com.codecommit.antixml.Group copy$default$5();
public scala.collection.immutable.Map copy$default$4();
public com.codecommit.antixml.Attributes copy$default$3();
public java.lang.String copy$default$2();
public scala.Option copy$default$1();
public com.codecommit.antixml.Elem copy(scala.Option, java.lang.String, com.codecommit.antixml.Attributes, scala.collection.immutable.Map, com.codecommit.antixml.Group);
public int hashCode();
public boolean equals(java.lang.Object);
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public boolean canEqual(java.lang.Object);
public com.codecommit.antixml.Elem(scala.Option, java.lang.String, com.codecommit.antixml.Attributes, scala.collection.immutable.Map, com.codecommit.antixml.Group);
}
成功!!!
當然,這次會議是謊言。我到達那裏需要更長的時間,但這主要是練習。
https://github.com/ensime/ensime-server/blob/master/build.sbt#L35
// epic hack to get the tools.jar JDK dependency
val JavaTools = List[Option[String]] (
// manual
sys.env.get("JDK_HOME"),
sys.env.get("JAVA_HOME"),
// osx
try Some("/usr/libexec/java_home".!!.trim)
catch {
case _: Throwable => None
},
// fallback
sys.props.get("java.home").map(new File(_).getParent),
sys.props.get("java.home")
).flatten.map { n =>
new File(n + "/lib/tools.jar")
}.find(_.exists).getOrElse (
throw new FileNotFoundException (
"""Could not automatically find the JDK/lib/tools.jar.
|You must explicitly set JDK_HOME or JAVA_HOME.""".stripMargin
)
)
謝謝,丹尼爾,對於詳細的解答。 –
使用SBT 0.12.0我得到: $ sbt console scala> set fullClasspath in Compile + = Attributed.blank(file(「/ usr/lib/jvm/jdk1.7.0/lib/tools.jar」)) :1:錯誤:';'預計但''。找到。 在編譯+ = Attributed.blank(file(「/ usr/lib/jvm/jdk1.7.0/lib/tools.jar」))中設置fullClasspath –
不確定發生了哪個SBT版本,但現在依賴項是「compile: console :: fullClasspath'就像你想的那樣。編譯在控制檯中的fullClasspath工作。 –