Java执行Scala编译的jar包时,提示Exception in thread “main“ java.lang.NoClassDefFoundError

隆飞驰
2023-12-01

最近刚接触Spark,踩了一些当时觉得比较奇怪的坑,所幸摸索了一套有个人特色的解决方案,并以此管窥了一下Scala和Java这两种语言,特此归纳并记录。
下面的命令是在Windows 10+的Powershell下的,不过对其他平台应该也大同小异。

问题描述

直接运行Spark的examples或者其他Scala编译出的jar包,会报错:

&"G:\Program Files\Java\jdk1.7.0_80\bin\java.exe" -classpath "G:\GreenSoftwares\spark-2.1.0-bin-hadoop2.7\examples\jars\spark-examples_2.11-2.1.0.jar" org.apache.spark.examples.SparkPi 100

报错信息基本是找不到Scala相关的类和函数:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq
        at org.apache.spark.examples.SparkPi.main(SparkPi.scala)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 1 more

但是,通过spark-submit可以成功运行,如:

spark-submit.cmd --master 'local[*]'  --class org.apache.spark.examples.SparkPi .\spark-examples_2.11-2.1.0.jar 1000

通过IDEA一般也能成功执行到里面的前几句,之后会报错需要提供spark master URL,说明至少能跑了。

解决方案

这是因为class path缺少scala相关的jar包,因此参考IDEA运行参数,如果非要通过Java直接执行,前文的命令应改为

java -cp <一堆scala相关的jar包,中间用英文分号(;)隔开;spark-examples_2.11-2.1.0.jar> org.apache.spark.examples.SparkPi 1000

例如(下面-classpath非常长,而且基本是和我的环境相关的路径,直接复制肯定没用):

&"G:\Program Files\Java\jdk1.7.0_80\bin\java.exe" -classpath "G:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\deploy.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\access-bridge-64.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\dnsns.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\jaccess.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\localedata.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunec.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunjce_provider.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunmscapi.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\zipfs.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\javaws.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\jfxrt.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\management-agent.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\plugin.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;G:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;F:\changeworld\2018National-College-Competition-on-Cloud-Computing\target\classes;H:\Users\Zhang\.m2\repository\org\scala-lang\scala-library\2.10.4\scala-library-2.10.4.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-core_2.10\2.1.0\spark-core_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\avro\avro-mapred\1.7.7\avro-mapred-1.7.7-hadoop2.jar;H:\Users\Zhang\.m2\repository\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7.jar;H:\Users\Zhang\.m2\repository\org\apache\avro\avro\1.7.7\avro-1.7.7.jar;H:\Users\Zhang\.m2\repository\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7-tests.jar;H:\Users\Zhang\.m2\repository\org\codehaus\jackson\jackson-core-asl\1.9.13\jackson-core-asl-1.9.13.jar;H:\Users\Zhang\.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.13\jackson-mapper-asl-1.9.13.jar;H:\Users\Zhang\.m2\repository\com\twitter\chill_2.10\0.8.0\chill_2.10-0.8.0.jar;H:\Users\Zhang\.m2\repository\com\esotericsoftware\kryo-shaded\3.0.3\kryo-shaded-3.0.3.jar;H:\Users\Zhang\.m2\repository\com\esotericsoftware\minlog\1.3.0\minlog-1.3.0.jar;H:\Users\Zhang\.m2\repository\org\objenesis\objenesis\2.1\objenesis-2.1.jar;H:\Users\Zhang\.m2\repository\com\twitter\chill-java\0.8.0\chill-java-0.8.0.jar;H:\Users\Zhang\.m2\repository\org\apache\xbean\xbean-asm5-shaded\4.4\xbean-asm5-shaded-4.4.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-client\2.2.0\hadoop-client-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-common\2.2.0\hadoop-common-2.2.0.jar;H:\Users\Zhang\.m2\repository\commons-cli\commons-cli\1.2\commons-cli-1.2.jar;H:\Users\Zhang\.m2\repository\org\apache\commons\commons-math\2.1\commons-math-2.1.jar;H:\Users\Zhang\.m2\repository\xmlenc\xmlenc\0.52\xmlenc-0.52.jar;H:\Users\Zhang\.m2\repository\commons-io\commons-io\2.1\commons-io-2.1.jar;H:\Users\Zhang\.m2\repository\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;H:\Users\Zhang\.m2\repository\commons-configuration\commons-configuration\1.6\commons-configuration-1.6.jar;H:\Users\Zhang\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;H:\Users\Zhang\.m2\repository\commons-digester\commons-digester\1.8\commons-digester-1.8.jar;H:\Users\Zhang\.m2\repository\commons-beanutils\commons-beanutils\1.7.0\commons-beanutils-1.7.0.jar;H:\Users\Zhang\.m2\repository\commons-beanutils\commons-beanutils-core\1.8.0\commons-beanutils-core-1.8.0.jar;H:\Users\Zhang\.m2\repository\com\google\protobuf\protobuf-java\2.5.0\protobuf-java-2.5.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-auth\2.2.0\hadoop-auth-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\commons\commons-compress\1.4.1\commons-compress-1.4.1.jar;H:\Users\Zhang\.m2\repository\org\tukaani\xz\1.0\xz-1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-hdfs\2.2.0\hadoop-hdfs-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\mortbay\jetty\jetty-util\6.1.26\jetty-util-6.1.26.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-app\2.2.0\hadoop-mapreduce-client-app-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-common\2.2.0\hadoop-mapreduce-client-common-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-yarn-client\2.2.0\hadoop-yarn-client-2.2.0.jar;H:\Users\Zhang\.m2\repository\com\google\inject\guice\3.0\guice-3.0.jar;H:\Users\Zhang\.m2\repository\javax\inject\javax.inject\1\javax.inject-1.jar;H:\Users\Zhang\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-yarn-server-common\2.2.0\hadoop-yarn-server-common-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-shuffle\2.2.0\hadoop-mapreduce-client-shuffle-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-yarn-api\2.2.0\hadoop-yarn-api-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-core\2.2.0\hadoop-mapreduce-client-core-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-yarn-common\2.2.0\hadoop-yarn-common-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-mapreduce-client-jobclient\2.2.0\hadoop-mapreduce-client-jobclient-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\hadoop\hadoop-annotations\2.2.0\hadoop-annotations-2.2.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-launcher_2.10\2.1.0\spark-launcher_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-network-common_2.10\2.1.0\spark-network-common_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\fusesource\leveldbjni\leveldbjni-all\1.8\leveldbjni-all-1.8.jar;H:\Users\Zhang\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.6.5\jackson-annotations-2.6.5.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-network-shuffle_2.10\2.1.0\spark-network-shuffle_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-unsafe_2.10\2.1.0\spark-unsafe_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\net\java\dev\jets3t\jets3t\0.7.1\jets3t-0.7.1.jar;H:\Users\Zhang\.m2\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar;H:\Users\Zhang\.m2\repository\commons-httpclient\commons-httpclient\3.1\commons-httpclient-3.1.jar;H:\Users\Zhang\.m2\repository\org\apache\curator\curator-recipes\2.4.0\curator-recipes-2.4.0.jar;H:\Users\Zhang\.m2\repository\org\apache\curator\curator-framework\2.4.0\curator-framework-2.4.0.jar;H:\Users\Zhang\.m2\repository\org\apache\curator\curator-client\2.4.0\curator-client-2.4.0.jar;H:\Users\Zhang\.m2\repository\org\apache\zookeeper\zookeeper\3.4.5\zookeeper-3.4.5.jar;H:\Users\Zhang\.m2\repository\com\google\guava\guava\14.0.1\guava-14.0.1.jar;H:\Users\Zhang\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;H:\Users\Zhang\.m2\repository\org\apache\commons\commons-math3\3.4.1\commons-math3-3.4.1.jar;H:\Users\Zhang\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;H:\Users\Zhang\.m2\repository\org\slf4j\slf4j-api\1.7.16\slf4j-api-1.7.16.jar;H:\Users\Zhang\.m2\repository\org\slf4j\jul-to-slf4j\1.7.16\jul-to-slf4j-1.7.16.jar;H:\Users\Zhang\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.16\jcl-over-slf4j-1.7.16.jar;H:\Users\Zhang\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;H:\Users\Zhang\.m2\repository\org\slf4j\slf4j-log4j12\1.7.16\slf4j-log4j12-1.7.16.jar;H:\Users\Zhang\.m2\repository\com\ning\compress-lzf\1.0.3\compress-lzf-1.0.3.jar;H:\Users\Zhang\.m2\repository\org\xerial\snappy\snappy-java\1.1.2.6\snappy-java-1.1.2.6.jar;H:\Users\Zhang\.m2\repository\net\jpountz\lz4\lz4\1.3.0\lz4-1.3.0.jar;H:\Users\Zhang\.m2\repository\org\roaringbitmap\RoaringBitmap\0.5.11\RoaringBitmap-0.5.11.jar;H:\Users\Zhang\.m2\repository\commons-net\commons-net\2.2\commons-net-2.2.jar;H:\Users\Zhang\.m2\repository\org\json4s\json4s-jackson_2.10\3.2.11\json4s-jackson_2.10-3.2.11.jar;H:\Users\Zhang\.m2\repository\org\json4s\json4s-core_2.10\3.2.11\json4s-core_2.10-3.2.11.jar;H:\Users\Zhang\.m2\repository\org\json4s\json4s-ast_2.10\3.2.11\json4s-ast_2.10-3.2.11.jar;H:\Users\Zhang\.m2\repository\com\thoughtworks\paranamer\paranamer\2.6\paranamer-2.6.jar;H:\Users\Zhang\.m2\repository\org\scala-lang\scalap\2.10.0\scalap-2.10.0.jar;H:\Users\Zhang\.m2\repository\org\scala-lang\scala-compiler\2.10.0\scala-compiler-2.10.0.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\core\jersey-client\2.22.2\jersey-client-2.22.2.jar;H:\Users\Zhang\.m2\repository\javax\ws\rs\javax.ws.rs-api\2.0.1\javax.ws.rs-api-2.0.1.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\hk2-api\2.4.0-b34\hk2-api-2.4.0-b34.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\hk2-utils\2.4.0-b34\hk2-utils-2.4.0-b34.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\external\aopalliance-repackaged\2.4.0-b34\aopalliance-repackaged-2.4.0-b34.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\external\javax.inject\2.4.0-b34\javax.inject-2.4.0-b34.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\hk2-locator\2.4.0-b34\hk2-locator-2.4.0-b34.jar;H:\Users\Zhang\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\core\jersey-common\2.22.2\jersey-common-2.22.2.jar;H:\Users\Zhang\.m2\repository\javax\annotation\javax.annotation-api\1.2\javax.annotation-api-1.2.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\bundles\repackaged\jersey-guava\2.22.2\jersey-guava-2.22.2.jar;H:\Users\Zhang\.m2\repository\org\glassfish\hk2\osgi-resource-locator\1.0.1\osgi-resource-locator-1.0.1.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\core\jersey-server\2.22.2\jersey-server-2.22.2.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\media\jersey-media-jaxb\2.22.2\jersey-media-jaxb-2.22.2.jar;H:\Users\Zhang\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet\2.22.2\jersey-container-servlet-2.22.2.jar;H:\Users\Zhang\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet-core\2.22.2\jersey-container-servlet-core-2.22.2.jar;H:\Users\Zhang\.m2\repository\io\netty\netty-all\4.0.42.Final\netty-all-4.0.42.Final.jar;H:\Users\Zhang\.m2\repository\io\netty\netty\3.8.0.Final\netty-3.8.0.Final.jar;H:\Users\Zhang\.m2\repository\com\clearspring\analytics\stream\2.7.0\stream-2.7.0.jar;H:\Users\Zhang\.m2\repository\io\dropwizard\metrics\metrics-core\3.1.2\metrics-core-3.1.2.jar;H:\Users\Zhang\.m2\repository\io\dropwizard\metrics\metrics-jvm\3.1.2\metrics-jvm-3.1.2.jar;H:\Users\Zhang\.m2\repository\io\dropwizard\metrics\metrics-json\3.1.2\metrics-json-3.1.2.jar;H:\Users\Zhang\.m2\repository\io\dropwizard\metrics\metrics-graphite\3.1.2\metrics-graphite-3.1.2.jar;H:\Users\Zhang\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.6.5\jackson-databind-2.6.5.jar;H:\Users\Zhang\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.6.5\jackson-core-2.6.5.jar;H:\Users\Zhang\.m2\repository\com\fasterxml\jackson\module\jackson-module-scala_2.10\2.6.5\jackson-module-scala_2.10-2.6.5.jar;H:\Users\Zhang\.m2\repository\org\scala-lang\scala-reflect\2.10.6\scala-reflect-2.10.6.jar;H:\Users\Zhang\.m2\repository\com\fasterxml\jackson\module\jackson-module-paranamer\2.6.5\jackson-module-paranamer-2.6.5.jar;H:\Users\Zhang\.m2\repository\org\apache\ivy\ivy\2.4.0\ivy-2.4.0.jar;H:\Users\Zhang\.m2\repository\oro\oro\2.0.8\oro-2.0.8.jar;H:\Users\Zhang\.m2\repository\net\razorvine\pyrolite\4.13\pyrolite-4.13.jar;H:\Users\Zhang\.m2\repository\net\sf\py4j\py4j\0.10.4\py4j-0.10.4.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-tags_2.10\2.1.0\spark-tags_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\scalatest\scalatest_2.10\2.2.6\scalatest_2.10-2.2.6.jar;H:\Users\Zhang\.m2\repository\org\apache\commons\commons-crypto\1.0.0\commons-crypto-1.0.0.jar;H:\Users\Zhang\.m2\repository\org\spark-project\spark\unused\1.0.0\unused-1.0.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-mllib_2.10\2.1.0\spark-mllib_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-streaming_2.10\2.1.0\spark-streaming_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-sql_2.10\2.1.0\spark-sql_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\com\univocity\univocity-parsers\2.2.1\univocity-parsers-2.2.1.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-sketch_2.10\2.1.0\spark-sketch_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-catalyst_2.10\2.1.0\spark-catalyst_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\codehaus\janino\janino\3.0.0\janino-3.0.0.jar;H:\Users\Zhang\.m2\repository\org\codehaus\janino\commons-compiler\3.0.0\commons-compiler-3.0.0.jar;H:\Users\Zhang\.m2\repository\org\antlr\antlr4-runtime\4.5.3\antlr4-runtime-4.5.3.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-column\1.8.1\parquet-column-1.8.1.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-common\1.8.1\parquet-common-1.8.1.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-encoding\1.8.1\parquet-encoding-1.8.1.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-hadoop\1.8.1\parquet-hadoop-1.8.1.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-format\2.3.0-incubating\parquet-format-2.3.0-incubating.jar;H:\Users\Zhang\.m2\repository\org\apache\parquet\parquet-jackson\1.8.1\parquet-jackson-1.8.1.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-graphx_2.10\2.1.0\spark-graphx_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\com\github\fommil\netlib\core\1.1.2\core-1.1.2.jar;H:\Users\Zhang\.m2\repository\net\sourceforge\f2j\arpack_combined_all\0.1\arpack_combined_all-0.1.jar;H:\Users\Zhang\.m2\repository\org\apache\spark\spark-mllib-local_2.10\2.1.0\spark-mllib-local_2.10-2.1.0.jar;H:\Users\Zhang\.m2\repository\org\scalanlp\breeze_2.10\0.12\breeze_2.10-0.12.jar;H:\Users\Zhang\.m2\repository\org\scalanlp\breeze-macros_2.10\0.12\breeze-macros_2.10-0.12.jar;H:\Users\Zhang\.m2\repository\org\scalamacros\quasiquotes_2.10\2.0.0-M8\quasiquotes_2.10-2.0.0-M8.jar;H:\Users\Zhang\.m2\repository\net\sf\opencsv\opencsv\2.3\opencsv-2.3.jar;H:\Users\Zhang\.m2\repository\com\github\rwl\jtransforms\2.4.0\jtransforms-2.4.0.jar;H:\Users\Zhang\.m2\repository\org\spire-math\spire_2.10\0.7.4\spire_2.10-0.7.4.jar;H:\Users\Zhang\.m2\repository\org\spire-math\spire-macros_2.10\0.7.4\spire-macros_2.10-0.7.4.jar;H:\Users\Zhang\.m2\repository\com\chuusai\shapeless_2.10.4\2.0.0\shapeless_2.10.4-2.0.0.jar;H:\Users\Zhang\.m2\repository\org\jpmml\pmml-model\1.2.15\pmml-model-1.2.15.jar;H:\Users\Zhang\.m2\repository\org\jpmml\pmml-schema\1.2.15\pmml-schema-1.2.15.jar;G:\GreenSoftwares\spark-2.1.0-bin-hadoop2.7\examples\jars\spark-examples_2.11-2.1.0.jar" org.apache.spark.examples.SparkPi 100

此时,就只会提示输入spark master了(即调用参数不足,但已经能正常执行了)。

 类似资料: