简体   繁体   English

我如何让hadoop使用与`javac`相同的Java Runtime版本?

[英]How can I make hadoop use the same Java Runtime version as `javac`?

I try to see if hadoop's own command can run a map reduce application (from the Hadoop's Map Reduce official document) compiled directly by javac instead of hadoop's own command. 我试着看看hadoop自己的命令是否可以运行javac直接编译的map减少应用程序(来自Hadoop的Map Reduce官方文档)而不是hadoop自己的命令。

I compile a map reduce application compiled directly by javac 我编译了一个由javac直接编译的map reduce应用程序

$ javac -classpath "$(~/programs/hadoop/hadoop-3.1.2/bin/hadoop classpath)" -d target/classes/myapp/ src/main/java/myapp/WordCount.java

and create a jar file 并创建一个jar文件

$ jar -cvf WordCount.jar -C target/classes/myapp/ .

Why can't I run the jar file by hadoop's own command? 为什么我不能通过hadoop自己的命令运行jar文件?

$ export HADOOP_CLASSPATH=$JAVA_HOME/lib/tools.jar
$ rm -r ./output/
$ ~/programs/hadoop/hadoop-3.1.2/bin/hadoop jar WordCount.jar WordCount ./input/ ./output/
Exception in thread "main" java.lang.UnsupportedClassVersionError: WordCount has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:311)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:232)

How can I make hadoop jar use the same Java Runtime version as javac ? 如何让hadoop jar使用与javac相同的Java Runtime版本?

Thanks. 谢谢。

You compiled it with a newer version of Java (looks like Java SE 10?) than what hadoop is running. 您使用较新版本的Java(看起来像Java SE 10?)编译它,而不是运行hadoop。 Either: 或者:

  1. Compile your Java program to an older version of java using the -target flag. 使用-target标志将Java程序编译为旧版本的java。 ie. 即。 -target 1.8
  2. Have hadoop use the same Java runtime that you compiled your program with. hadoop使用与编译程序相同的Java运行时。 There should be a hadoop-env.sh file that gets called by hadoop to set the JAVA_HOME. 应该有一个hadoop调用的hadoop-env.sh文件来设置JAVA_HOME。

Under your HADOOP_HOME/conf directory please update the hadoop-env.sh file. 在您的HADOOP_HOME / conf目录下,请更新hadoop-env.sh文件。 It has entry to export JAVA_HOME. 它有导出JAVA_HOME的条目。

Setting to appropriate JAVA_HOME in this file should solve your issue. 在此文件中设置适当的JAVA_HOME应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM