2011-12-07 10 views
18

स्केल 2.9.1 कंसोल में 'javap' कमांड 'classpath' में tools.jar (जेडीके 6 से) की आवश्यकता है। Cmd-line से यह '-cp' तर्क या क्लासस्पैट पर्यावरण चर के साथ किया जा सकता है।sbt प्रोजेक्ट के क्लासपाथ में tools.jar जोड़ें

एसबीटी से 'कंसोल' और 'कंसोल-क्विक' कमांड के साथ स्काला कंसोल के लिए ऐसा कैसे किया जाए?

उत्तर

31

लंबा उत्तर, जो आपको कहीं और मदद कर सकता है।

अगर मैं एसबीटी में किसी चीज के बारे में जानना चाहते हैं, मैं इसे 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 बन जाएगा। Compile में अपरकेस नोट करें, और full-classpathfullClasspath बन गया, और तत्व नामों के सामान्य पुनर्गठन। Details here

मैं (अब के लिए वैसे भी) लगता है कि एक (, inspect या कम से कम,) show के उत्पादन लेने के लिए और यह सही set वापस करने के लिए फ़ीड करने के लिए सक्षम होना चाहिए, लेकिन यह मामला नहीं है, तो बस ये रूपांतरण नियम सीखना ।

मैं सबकुछ फिर से टाइप नहीं करना चाहता, बस एक जेएआर फ़ाइल जोड़ें। मुझे इसके लिए += का उपयोग करने की आवश्यकता है। चीजों को बदलने के लिए प्रयुक्त ऑपरेटरों को here मिल सकता है।

क्लासपाथ को कुछ Attributed सामान की आवश्यकता है। एसबीटी विकी में 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); 
} 

सफलता !!!

बेशक, यह सत्र झूठ है। मेरे पास वहां पहुंचने में काफी समय लगा, लेकिन यह ज्यादातर अभ्यास है।

+0

आप विस्तृत जवाब के लिए धन्यवाद, डैनियल,। –

+0

एसबीटी 0.12.0 के साथ मैं मिलता है: $ एसबीटी सांत्वना स्केला> संकलित में fullClasspath सेट + = Attributed.blank (फाइल "(/ usr/lib/JVM/jdk1.7.0/lib/tools.jar")) : 1: त्रुटि: ';' अपेक्षित लेकिन '।' मिल गया। संकलन में पूर्ण क्लासपाथ सेट करें + = विशेषता.ब्लैंक (फ़ाइल ("/ usr/lib/jvm/jdk1.7.0/lib/tools.jar")) –

+1

यह सुनिश्चित नहीं है कि यह कौन सा एसबीटी संस्करण हुआ, लेकिन अब निर्भरता 'संकलित है: कंसोल :: fullClasspath' जैसा आप चाहते थे। कंसोल में संकलन में पूर्ण क्लासपाथ 'काम करता है। –

6

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 
) 
) 
संबंधित मुद्दे