从 Intelli-J 构建 jar 将无法执行

我整个晚上都在挖掘有关此内容的帖子,似乎找不到适合我的解决方案。我设置了我的工件,构建了我的罐子,运行了“java -jar myProject.jar”并收到了这个:


Error: Could not find or load main class com.myProject.MyProject

Caused by: java.lang.ClassNotFoundException: com.myProject.MyProject

我已经提取了jar文件,类文件就在那里。我对Java还比较陌生,所以我不确定这是否相关,但是如果我点击编辑“MyProject.class”,在jar文件中,内容看起来像这样:


Êþº¾

除此之外,我不知道还能去哪里寻找。我的源目录设置正确,“com”包位于jar文件的第一层。


可能还值得注意的是,这是一个使用Maven的弹簧启动应用程序。


编辑:


上面我编辑了包到com.myProject.MyProject(为简单起见),但那里有另一层,这反映在下面的代码中,“com.myProject.myProject.MyProject”。抱歉,如果这令人困惑:/


我的项目.java:


package com.myProject.myProject;


// imports excluded for brevity


@SpringBootApplication

public class MyProject {


public static void main(String[] args) {

    SpringApplication.run(MyProject.class, args);

}


// Fix the CORS errors

@Bean

public FilterRegistrationBean simpleCorsFilter() {

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

    CorsConfiguration config = new CorsConfiguration();

    config.setAllowCredentials(true);

    // *** URL below needs to match the Vue client URL and port ***

    config.setAllowedOrigins(Collections.singletonList("http://localhost:8080"));

    config.setAllowedMethods(Collections.singletonList("*"));

    config.setAllowedHeaders(Collections.singletonList("*"));

    source.registerCorsConfiguration("/**", config);

    FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source));

    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

    return bean;

}

}


慕尼黑的夜晚无繁华
浏览 89回答 1
1回答

慕村225694

弹簧启动要求您在 pom 中的属性标记中指定主类.xml<start-class><properties><!-- The main class to start by executing "java -jar"--><start-class>com.myProject.MyProject</start-class></properties>编辑根据您的pom.xml文件,您的软件包结构应如下所示如果您确定您的应用程序只有一个主要入口点。然后尝试更新您的起始pom.xml以下基本依赖项,并在执行生成的jar之前执行。mvn clean install&nbsp; &nbsp; <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.3.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; <relativePath/> <!-- lookup parent from repository -->&nbsp; &nbsp; </parent>&nbsp; &nbsp; <groupId>com.myProject</groupId>&nbsp; &nbsp; <artifactId>myProject</artifactId>&nbsp; &nbsp; <version>0.0.1-SNAPSHOT</version>&nbsp; &nbsp; <name>myproject</name>&nbsp; &nbsp; <description>My Project</description>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build></project>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java