简体   繁体   English

Java 运行 jar 文件时找不到或加载主 class

[英]Java Could not find or load main class when running jar file

I have a springboot application, it runs just fine in intellij.我有一个 springboot 应用程序,它在 intellij 中运行得很好。 I'm trying to create an executable jar to run the application on other machines.我正在尝试创建一个可执行文件 jar 以在其他计算机上运行该应用程序。 I followed this我跟着这个

tutorialhttps://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/教程 https://www.jetbrains.com/idea/guide/tutorials/hello-world/packaging-the-application/

down to every step, and managed to build my jar. but when i try to run the jar with深入到每一步,并设法构建我的 jar。但是当我尝试运行 jar 时

java -jar nameofjar.java

I get a Could not find or load main class error.我收到 Could not find or load main class 错误。 I unzipped the jar file with java c and the classpath correctly points to the main class, so im really lost as to what i have to fix here to get java to detect the main class.我用 java c 解压缩了 jar 文件,类路径正确指向主要的 class,所以我真的不知道我必须在这里修复什么才能得到 java 来检测主要的 class。

I've tried a bunch of solutions from here:我从这里尝试了很多解决方案:

Error: Could not find or load main class in intelliJ IDE 错误:无法在 intelliJ IDE 中找到或加载 main class

such as deleting.idea or Rebuild Project or doing mvn clean package but nothing seems to work.例如 deleting.idea 或 Rebuild Project 或执行 mvn clean package 但似乎没有任何效果。

Using java -c to open the jar file and verify that the main classpath is indeed correct使用java -c打开jar文件,验证主类路径确实正确在此处输入图像描述

Main class and file structure:主要class及文件结构: 主类

This run config works perfectly fine in intellij这个运行配置在 intellij 中工作得很好在此处输入图像描述

here is my manifest.mf:这是我的 manifest.mf:

Manifest-Version: 1.0
Main-Class: com.owl.PosApi.OwlPosApiApplication

If you build jar artifact using IntelliJ IDEA, it may be incomplete or manifests from the other included dependencies can override your custom one with the main class defined.如果您使用 IntelliJ IDEA 构建 jar 工件,它可能不完整,或者来自其他包含的依赖项的清单可以覆盖您定义的主要 class 的自定义项。

Related answers:相关回答:

The proper solution would be to build the artifact using Maven. It may work out of the box with mvn clean package or you may need to modify pom.xml to build the fat jar with all the dependencies per this document .正确的解决方案是使用 Maven 构建工件。它可能开箱即用mvn clean package或者您可能需要修改pom.xml以构建 fat jar 以及此文档中的所有依赖项。

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

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