Java 错误:尝试在 OS X 上运行 Cassandra 时 .class 文件中的错误版本号错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1611312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:47:40  来源:igfitidea点击:

Java error: Bad version number in .class file error when trying to run Cassandra on OS X

java

提问by Sam Lee

I am trying to get Cassandra to work on OS X. When I run bin/cassandra, I get the following error:

我试图让 Cassandra 在 OS X 上工作。当我运行时bin/cassandra,我收到以下错误:

~/apache-cassandra-incubating-0.4.1-src > bin/cassandra -f
Listening for transport dt_socket at address: 8888
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
 at java.net.URLClassLoader.access0(URLClassLoader.java:56)
 at java.net.URLClassLoader.run(URLClassLoader.java:195)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)

From what I could determine by searching, this error is related to incompatible versions of Java. However, as far as I can tell, I have the latest version of Java:

根据我通过搜索确定的情况,此错误与 Java 版本不兼容有关。但是,据我所知,我拥有最新版本的 Java:

    ~/apache-cassandra-incubating-0.4.1-src > java -version
    java version "1.6.0_13"
    Java(TM) SE Runtime Environment (build 1.6.0_13-b03-211)
    Java HotSpot(TM) 64-Bit Server VM (build 11.3-b02-83, mixed mode)
    ~/apache-cassandra-incubating-0.4.1-src > javac -version
    javac 1.6.0_13
    ~/Downloads/apache-cassandra-incubating-0.4.1-src > echo $JAVA_HOME
    /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

Any ideas on what I'm doing wrong?

关于我做错了什么的任何想法?

采纳答案by paxdiablo

The bad version number is almost alwaysbecause you've compiled your java file to a class file with one version and are trying to run it with an earlier version.

错误的版本号几乎总是因为您已将 java 文件编译为具有一个版本的类文件,并尝试使用早期版本运行它。

You need to be certain the this "cassandra" is using the Java version you think it is. It's not necessarily using the same one you get when running java from the command prompt.

您需要确定这个“cassandra”正在使用您认为的 Java 版本。它不一定使用与从命令提示符运行 java 时获得的相同的方法。

回答by Philip Schlump

In cassandra/bin in the file cassandra.in.sh add the following 2 lines at the bottom of the file:

在文件 cassandra.in.sh 的 cassandra/bin 中,在文件底部添加以下两行:

JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
JAVA=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Commands/java

This will be enough to get bin/cassandra to run. To get the other shell scripts to work with this you may need patch 590 and fix the other shell scripts to use JAVA_HOME and JAVA variables.

这足以让 bin/cassandra 运行。要让其他 shell 脚本与此一起工作,您可能需要补丁 590 并修复其他 shell 脚本以使用 JAVA_HOME 和 JAVA 变量。

回答by shaival

If you using maven put in pom.xml, so this way it will compile by 1.5 even if u use 1.6

如果您使用 maven 放入 pom.xml,那么即使您使用 1.6,它也会通过 1.5 编译

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
      <debug>true</debug>
    </configuration>
  </plugin>