0

I'm working now on a maven-enabled project, using Eclipse IDE. It builds, but I can run.

When I build it, I have this on my IDE console:

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] ------------------------------------------------------------------------

When I try to run it through cmd, I got this:

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

My pom.xml is as following:

<?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>

What could be done to make the project run smoothly? I already saw this answer, but the answer given there did not help me. Could someone explain it to me better?

  • The .jar produced by your build does not contain the dependencies, and those are not available in your run environment. Hence, as soon as the first dependency is missing (in your case LoggerFactory from sl4j), the code fails to load the class. You may want to produce a [fat jar](https://stackoverflow.com/a/574650/3111149) instead (fat = all dependencies packaged within the same jar, so that your jar is runnable on standalone). – Matteo NNZ Jul 20 '20 at 22:39
  • Thank you for your answer. How, more specifically, can I do this? Should I use which code in the post you linked: the first or the second? And where in the pom.xml file should I paste these codes? Forgive my doubts, I don't have much experience with this type of tool. – Higo Felipe Silva Pires Jul 20 '20 at 22:59
  • You need to add the plugin "maven-assembly-plugin" to your pom.xml as specified in the answer I linked. – Matteo NNZ Jul 20 '20 at 23:07
  • Never try to handle version of spring yourself. You are using the spring-boot parent so let handle that spring boot and do not override the versions of spring by yourself. Also you have defined maven-compiler-plugin duplicate and defined different java versions you want to compile? See the answer... – khmarbaise Jul 21 '20 at 18:46

1 Answers1

2

I see a number of errors:

  1. You have 2 different maven-compiler-plugin configurations, while you don't need either of them - just remove both. The correct configuration is defined in spring-boot-starter-parent and you tune it by specifying <java.version>1.8</java.version>.
  2. Similarly plugins maven-jar-plugin and exec-maven-plugin does not seem useful either.
  3. Instead of the above mentioned plugins you do need to configure spring-boot-maven-plugin to make your life easier, e.g.:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  1. Avoid specifying <version> for each and every dependency. Most of them are managed by Spring Boot - it's one of the key features. You can see currently managed dependencies with their versions here.
  2. You don't need joda-time in Java 8, just use java.time intead. More details here.
  3. Why do you use both Jackson and GSON. Both of them handle JSON processing, so why don't you focus on one of them?

Consider spending more time with Spring Boot's documentation to use this great tool more effectively.

stepio
  • 865
  • 2
  • 9
  • 22
  • All of these tips are right, +1. However, I don't think any of them will solve the issue which is just the dependencies are missing at runtime. You may want to add a point about that too. – Matteo NNZ Jul 20 '20 at 23:08
  • I'm not 100% sure, but won't #3 produce fat jar and solve the dependency problem? – stepio Jul 20 '20 at 23:10
  • 1
    I think you're right, I had read too fast. Let's see what the user said, good answer btw. – Matteo NNZ Jul 20 '20 at 23:22
  • Worked very well! Thank you! Point # 3 could not be implemented: the IDE, for some reason, was reporting an error when I removed this information; I ended up leaving, after all. – Higo Felipe Silva Pires Jul 21 '20 at 02:18