无法执行maven.jar

olhwl3o2  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(246)

作为序言(你可以跳过这个),我是一个相对缺乏经验的程序员(相对于一个行业专业人士),我想尝试使用maven作为依赖关系管理器,作为一个更好的方法来生成自包含的.jar文件,为我的高级科学展项目。
(如果您跳过了上一段,请从这里开始阅读)我能够清理、验证、编译和打包我的maven项目。jar文件是生成的,在我的文件资源管理器中它被识别为可执行的.jar文件。我正在使用JDK11.0.2。当我在我的文件资源管理器中双击.jar文件时,我的光标会在没有发生任何事情之前的一瞬间旋转一个蓝色的小圆圈“im doing something”。当我尝试使用

java -jar Project-Name-alpha.jar

我得到以下错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/CompanyName/Main has been compiled by a more recent version of the Java Runtime (class file version 55.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(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

我的pom.xml文件中的相关部分是:

<groupId>com.CompanyName</groupId>
  <artifactId>ProjectName</artifactId>
  <version>alpha</version>
  <packaging>jar</packaging>

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>dependency-jars/</classpathPrefix>
          <mainClass>com.FoxInnovations.Main</mainClass>
        </manifest>
       </archive>
     </configuration>
  </plugin>

   <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> 
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
          <goal>copy-dependencies</goal>
      </goals>
      <configuration>
          <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
      </configuration>
        </execution>
    </executions>
      </plugin>        <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> 
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
      <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
          <goal>copy-dependencies</goal>
      </goals>
      <configuration>
          <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
      </configuration>
        </execution>
    </executions>
      </plugin>

我认为问题可能是我没有正确的javajre,但是找不到jre11,我认为jdks通常包括相应的jre。
我敢打赌,我可能犯了一些我忽略了的错误。
如果我错误地没有包括一些重要的东西来解决这个问题,请让我知道,我会尽快添加它。

sg24os4d

sg24os4d1#

执行jar时使用的java版本低于用于编译的版本。很可能需要为maven编译器插件设置java目标和源代码。

<properties>
    <maven.compiler.source>1.11</maven.compiler.source>
    <maven.compiler.target>1.11</maven.compiler>target>
</properties>

相关问题