Search code examples
scalajson4s

json4s xpath report java.lang.NoSuchMethodError


I followed the example from "XPath + HOFs" part of https://github.com/json4s/json4s, below is my source code:

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))
}
}

no compile errors, but the following error occurred at runtime:

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)

Could anybody help me on this problem?

===How is the program invoked=== I use gradle to run above code, below is the build.gradle file(the dependencies are what is needed by other programs):

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[])
    }
}

and I run the following command to invoke the program:

gradle run -DmainClassName=HiveToCSVEngine

Solution

  • Before:

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

    Update:

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

    I think it's your scala is 2.11, but your json4s is 2.10