Building and executing JAR file error: could not find main class and unsupported major.minor version 52.0

Building and executing JAR file error: could not find main class and unsupported major.minor version 52.0

在 NetBeans 中构建 Java 程序后,我压缩了 dist 文件夹,将程序放在 USB 中。在另一台计算机上,提取所有文件后,我尝试 运行 JAR 文件,但 Window 提示说:

"Could not find the main class: logic.Main. Program will exit."

研究并尝试了类似问题的解决方案(即创建 Manifest 文件、创建 .bat 文件)但没有任何效果。

然后我在命令提示符下 运行 它,结果如下:

是否有 2 个问题:找不到主 class 以及在另一台计算机上 Java 没有更新?如何解决?

它实际上能够找到 a logic.Main,但无法加载它,因为它是用 Java 8 和用户机器编译的运行 是 Java 的早期版本。在 Java 的早期版本上编译文件或在目标计算机上更新 Java 将解决此问题。

有多种创建可执行jar的方法。

在 netbeans 中有一个选项

Project Properties -> Build -> Packaging -> Build JAR after compiling

Maven Build 也可用于创建可执行jar。在下面的 maven 插件中定义 main class。您也可以 select 编译器版本以避免主要的次要问题。

   <plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>com.kulhade.elasticsearch.Indexer</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
      <execution>
        <goals>
          <goal>one-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>