Java项目无法在Maven构建后运行并且不在cmd上运行

时间:2020-07-20 22:22:59

标签: java eclipse maven pom.xml slf4j

我现在正在使用Eclipse IDE处理启用了Maven的项目。它可以构建,但是我可以运行。

构建时,我在IDE控制台上拥有它:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/eclipse/configuration/org.eclipse.osgi/5/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/eclipse/configuration/org.eclipse.osgi/5/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.lsdi.social.mhealth:SocialMHealth:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin @ line 190, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------< com.lsdi.social.mhealth:SocialMHealth >----------------
[INFO] Building SocialMHealth 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] Downloading from : http://dist.codehaus.org/nz/ac/waikato/cms/weka/weka-dev/maven-metadata.xml
[INFO] Downloading from : http://snapshots.repository.codehaus.org/nz/ac/waikato/cms/weka/weka-dev/maven-metadata.xml
[WARNING] Could not transfer metadata nz.ac.waikato.cms.weka:weka-dev/maven-metadata.xml from/to codehaus (http://dist.codehaus.org/): dist.codehaus.org
[WARNING] Could not transfer metadata nz.ac.waikato.cms.weka:weka-dev/maven-metadata.xml from/to codehaus-snapshot (http://snapshots.repository.codehaus.org): snapshots.repository.codehaus.org
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ SocialMHealth ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Higo Felipe\git\SocialMHealth\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\Higo Felipe\git\SocialMHealth\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ SocialMHealth ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ SocialMHealth ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Higo Felipe\git\SocialMHealth\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ SocialMHealth ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ SocialMHealth ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ SocialMHealth ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ SocialMHealth ---
[INFO] Installing C:\Users\Higo Felipe\git\SocialMHealth\target\SocialMHealth-1.0-SNAPSHOT.jar to C:\Users\Higo Felipe\.m2\repository\com\lsdi\social\mhealth\SocialMHealth\1.0-SNAPSHOT\SocialMHealth-1.0-SNAPSHOT.jar
[INFO] Installing C:\Users\Higo Felipe\git\SocialMHealth\pom.xml to C:\Users\Higo Felipe\.m2\repository\com\lsdi\social\mhealth\SocialMHealth\1.0-SNAPSHOT\SocialMHealth-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.529 s
[INFO] Finished at: 2020-07-20T18:39:10-03:00
[INFO] ------------------------------------------------------------------------

当我尝试通过cmd运行它时,我得到了:

C:\Users\Higo Felipe\git\SocialMHealth\target>java -jar SocialMHealth-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.lsdi.social.mhealth.Start.<clinit>(Start.java:13)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 1 more

我的 pom.xml 如下:

<?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"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.lsdi.social.mhealth</groupId>
    <artifactId>SocialMHealth</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>Projeto Monitoramento Pervasivo da Saúde Mental</description>

    <properties>
        <java.version>1.8</java.version>
        <esper-version>5.1.0</esper-version>
        <spring-version>3.1.1.RELEASE</spring-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.0.13</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.2</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <dependency>
            <groupId>com.espertech</groupId>
            <artifactId>esper</artifactId>
            <version>${esper-version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1211.jre7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.28</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.28</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.5</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>nz.ac.waikato.cms.moa</groupId>
            <artifactId>moa</artifactId>
            <version>2019.05.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.3</version>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.lsdi.social.mhealth.Start</mainClass>
                    <includePluginDependencies>false</includePluginDependencies>
                    <arguments>
                        <argument>10000</argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.lsdi.social.mhealth.Start</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>codehaus</id>
            <name>Codehaus Release Repo</name>
            <url>http://dist.codehaus.org/</url>
        </repository>
        <repository>
            <id>codehaus-snapshot</id>
            <name>Codehaus Snapshot Repo</name>
            <url>http://snapshots.repository.codehaus.org</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>codehaus</id>
            <name>Codehaus Release Repo</name>
            <url>http://repository.codehaus.org</url>
        </pluginRepository>
        <pluginRepository>
            <id>codehaus-snapshot</id>
            <name>Codehaus Snapshot Repo</name>
            <url>http://snapshots.repository.codehaus.org</url>
        </pluginRepository>
    </pluginRepositories>

</project>

如何使项目顺利进行?我已经看到this的答案,但是那里给出的答案并没有帮助我。有人可以更好地向我解释吗?

1 个答案:

答案 0 :(得分:2)

我看到许多错误:

  1. 您有2种不同的maven-compiler-plugin配置,而您不需要它们两者-只需将它们都删除即可。在spring-boot-starter-parent中定义了正确的配置,您可以通过指定<java.version>1.8</java.version>对其进行调整。
  2. 类似地,插件maven-jar-pluginexec-maven-plugin似乎也没有用。
  3. 您确实需要配置spring-boot-maven-plugin来代替上面提到的插件,以使您的生活更轻松,例如:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  1. 避免为每个依赖项指定<version>。其中大多数由Spring Boot管理-这是关键功能之一。您可以看到当前托管的依赖项及其版本here
  2. 在Java 8中,您不需要joda-time,只需使用java.time intead。更多详细信息here
  3. 您为什么同时使用Jackson和GSON。它们都处理JSON处理,所以为什么不集中精力处理它们之一?

考虑将更多的时间花在Spring Boot的documentation上,以便更有效地使用这个出色的工具。