线程“main”中的异常 java.lang.NoSuchMethodError: org.apache.commons.beanutils.PropertyUtilsBean

the*_*ker 0 scala apache-spark apache-spark-sql

我正在尝试读取属性文件,但遇到了下面给出的错误。我编写了一个 Scala 包,我正在尝试读取属性文件并调用 abc.scala 程序。任何帮助将不胜感激。

文件:- xyz.properties

driver = "oracle.jdbc.driver.OracleDriver"
url = "jdbc:oracle:thin:@xxxx:1521/xxxx.xxxx"
username = "xxx"
password = "xxx"
input_file = "C:\\Users\\xxx\\test\\src\\main\\resources\\xxxx.xlsx"
Run Code Online (Sandbox Code Playgroud)

构建.sbt

name := "xxx.xxxx.xxxxx"

scalaVersion := "2.10.6"

ivyScala := ivyScala.value map{ _.copy(overrideScalaVersion = true) }


libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % "2.1.0",
"com.databricks" %% "spark-csv" % "1.5.0",
"org.apache.commons" % "commons-configuration2" % "2.1.1",
"commons-beanutils" % "commons-beanutils" % "1.9.3",
"org.apache.spark" %% "spark-sql" % "2.1.0",
"org.scala-lang" % "scala-xml" % "2.11.0-M4"  )
Run Code Online (Sandbox Code Playgroud)

包裹

package com.xxx.zzzz.xxx1

        import java.io.File
        import org.apache.commons.configuration2.builder.fluent.{Configurations, Parameters}


        object Configuration {

          var config = new Configurations()
          var configs = config.properties(new File("xyz.properties"))

          var inputFile = configs.getString("input")
          var userName = configs.getString("user_name")
          var password = configs.getString("passwd")
          var driver = configs.getString("driver")
          var url = configs.getString("Url")

        }
Run Code Online (Sandbox Code Playgroud)

主程序 abc.scala

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
import package com.xxx.zzzz.xxx1.Configurations
import org.apache.commons.beanutils.PropertyUtils


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

        //Logger.getRootLogger().setLevel(Level.OFF)

        println("Inside main program"+ Configuration.driver)
        //Set the properties for spark to connect the oracle database
        val dbProp = new java.util.Properties
        dbProp.setProperty("driver", Configuration.driver)
        dbProp.setProperty("user", Configuration.userName)
        dbProp.setProperty("password", Configuration.password)


        //Create a connection to connect spark
        val conf = new SparkConf().setAppName("Simple Application").setMaster("local")
        val sc = new SparkContext(conf)
        val sqlContext = new SQLContext(sc)

    //exception handlying
    try {
      //Create dataframe boject
      val df = sqlContext.read
        .option("location", Configuration.inputFile)        //Initiating input path
        .option("sheetName", "xyz")                       //Give the SheetName
        .option("useHeader", "true")                        //It takes the header name from excel sheet
        .option("treatEmptyValuesAsNulls", "true")
        .option("inferSchema", "true")
        .option("addColorColumns", "false")
        .load()
      // Write into oracale database
      df.write.mode("append").jdbc(Configuration.url, "xyz", dbProp)
    }
    catch {

        case e: Throwable => e.printStackTrace();
    }

      }
}
Run Code Online (Sandbox Code Playgroud)

错误

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector(Lorg/apache/commons/beanutils/BeanIntrospector;)V
at org.apache.commons.configuration2.beanutils.BeanHelper.initBeanUtilsBean(BeanHelper.java:631)
at org.apache.commons.configuration2.beanutils.BeanHelper.<clinit>(BeanHelper.java:89)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.sun.proxy.$Proxy0.<clinit>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739)
at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:294)
at org.apache.commons.configuration2.builder.fluent.Parameters.fileBased(Parameters.java:185)
at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:602)
at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:614)
at org.apache.commons.configuration2.builder.fluent.Configurations.fileBasedBuilder(Configurations.java:132)
at org.apache.commons.configuration2.builder.fluent.Configurations.propertiesBuilder(Configurations.java:238)
at org.apache.commons.configuration2.builder.fluent.Configurations.properties(Configurations.java:282)
at com.rxcorp.italy.config.Configuration$.<init>(Configuration.scala:8)
at com.rxcorp.italy.config.Configuration$.<clinit>(Configuration.scala)
at com.rxcorp.paneldataloading.ItalyPanelData$.main(abc.scala:12)
Run Code Online (Sandbox Code Playgroud)

Gho*_*ica 5

此类异常表明版本不兼容。

含义:您编写的代码(或更可能:表面下的库之一)想要调用一个方法

org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector(BeanIntrospector[]);
Run Code Online (Sandbox Code Playgroud)

但问题是:在运行时,PropertyUtilsBean 的类文件不包含该方法。

因此:您必须退后一步,了解堆栈中的组件,并在 Apache commons 库上检查它们的版本要求。

当您查看该方法的 javadoc 时,您会得到更多想法;正如它所说:自:1.9以来。

也就是说:Apache commons 1.9添加了这个方法;因此,您的堆栈中的某些部分至少需要该版本的公共资源;但是执行整个事情的 JVM 中的类路径...具有旧版本。

所以:检查 apache commons 的类路径;很可能您只需更新到较新版本的 apache commons 就可以了。(是的,也许这意味着更多“困难”的调试工作;因为至少您的构建设置包括较新版本的 apache commons)。