json4s xpath报告java.lang.NoSuchMethodError

时间:2015-03-14 07:56:22

标签: scala json4s

我按照https://github.com/json4s/json4s中“ XPath + HOFs ”部分的示例,下面是我的源代码:

import org.json4s._
import org.json4s.native.JsonMethods._

object HiveToCSVEngine {
def main(args: Array[String]): Unit = {

    val json = parse( """
     { "name": "joe",
       "children": [
         {
           "name": "Mary",
           "age": 5
         },
         {
           "name": "Mazy",
           "age": 3
         }
       ]
     }
                      """)
    print((json \ "children")(0))
}
}

没有编译错误,但在运行时发生了以下错误:

Exception in thread "main" java.lang.NoSuchMethodError:     
scala.collection.immutable.$colon$colon.hd$1()Ljava/lang/Object;
at org.json4s.MonadicJValue.$bslash(MonadicJValue.scala:18)

有人可以帮我解决这个问题吗?

===如何调用程序=== 我使用gradle运行上面的代码,下面是build.gradle文件(依赖项是其他程序所需的):

apply plugin: 'scala'
apply plugin: 'java'
apply plugin:'application'
//mainClassName = 'SimpleMail'
mainClassName = System.getProperty("mainClassName")
compileJava {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}
repositories {
    mavenCentral()
}
dependencies {
    //can't comment scalaTools on linux because of gradle version problem
    scalaTools "org.scala-lang:scala-compiler:2.11.1"
    compile 'org.scala-lang:scala-library:2.11.1'
    compile 'com.github.tototoshi:scala-csv_2.10:1.0.0'
    compile 'com.darwinsys:hirondelle-date4j:1.5.1'
    compile 'com.sun.mail:javax.mail:1.5.2'
    compile 'org.apache.hive:hive-jdbc:0.13.1'
    compile 'org.apache.hadoop:hadoop-core:1.2.1'
    compile 'mysql:mysql-connector-java:5.1.31'
    compile 'net.sourceforge.expectj:expectj:2.0.7'
    compile 'org.apache.commons:commons-lang3:3.3'
    compile 'org.json4s:json4s-native_2.10:3.2.10'
    compile 'org.json4s:json4s-jackson_2.10:3.2.10'
}
run {
    // set heap size for the test JVM(s)
    minHeapSize = "128m"
    maxHeapSize = "512m"
    // set JVM arguments for the test JVM(s)
    jvmArgs '-XX:MaxPermSize=256m'

    if ( project.hasProperty('args') ) {
        def paras=project.args.trim()
        println "args:"+paras
        def leading_paras=[]
        while(paras.contains('\'')){
            def prefix=paras.substring(0,paras.indexOf('\''))
            def right_part=paras.substring(prefix.size()+1)
            def middle=right_part.substring(0,right_part.indexOf('\''))
            def tail=right_part.substring(middle.size()+1)

            if(prefix.size()>0) {
                leading_paras = (leading_paras << prefix.split('\\s+')) <<     [middle]
            }else{
                leading_paras =leading_paras<<[middle]
            }
            paras=tail.trim()
        }
        leading_paras=(leading_paras<<paras.split('\\s+')).flatten()
        println "[param list]"
        for(String para:leading_paras){
            println "param:"+para+"."
        }
        args (leading_paras as String[])
    }
}

然后运行以下命令来调用程序:

gradle run -DmainClassName=HiveToCSVEngine

1 个答案:

答案 0 :(得分:0)

在:

  compile 'org.json4s:json4s-native_2.10:3.2.10'
  compile 'org.json4s:json4s-jackson_2.10:3.2.10'

更新

  compile 'org.json4s:json4s-native_2.11:3.2.10'
  compile 'org.json4s:json4s-jackson_2.11:3.2.10'

我认为这是scala 2.11,但您的json4s是2.10