I really do not know what is wrong with my Spring configuration. Could you give me hand?
My code (simplified version):
App.java:
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ctx.close();
}
}
Config.java:
#Configuration
#ComponentScan
public class Config {
#Bean
public JdbcOperations jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}
}
pom file:
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>4.2.5.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.185</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
My folder structure:
After I maven package it, when I run the jar I got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext
at App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.annotation.AnnotationConfigApplicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
in which, the line 10 is the AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
In addition:
I am writing the code when I was wathcing a spring tutorial:Spring Framework Tutorial 4: Database programming with JdbcTemplate
And his source code is at:github
The only difference with the source code is that I added the MainClass in the pom file.
Thank you so much!
Sincerely
The reason for this is, inside your jar you have only your classes without any dependencies. To include all the dependencies inside your jar you need to create a fat jar.
Replace your build block with following inside pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After running mvn clean package you will get two jar inside your target directory, first one is without dependency and the second one is with dependencies. Run the second jar.
CustomerJdbc-0.0.1-SNAPSHOT.jar
CustomerJdbc-0.0.1-SNAPSHOT-jar-with-dependencies.jar
The reason that you're getting that is because the maven-jar-plugin doesn't actually include any of the JARs that are referenced in the POM file by default.
There are a few ways that you can sort this out, this answer seems pretty comprehensive.
Also note that when referencing the main class in the POM, you need to reference it with the fully qualified class name (net.ubilife.spring.customerjdbc.App).
You can also run it with Shade to build the jar with the dependencies.
Just add "shade:shade" to the goals.
Example:
mvn clean install shade:shade
Related
I've been trying to run some simple code from the first lesson of R for Data Science using Renjin in Eclipse. The reason I'm using Renjin is because I'm eventually going to develop this into a Java data analysis program. Here is the link to the R for Data Science lesson I'm following: https://r4ds.had.co.nz/data-visualisation.html
I got all my dependencies in the pom.xml file of my Maven project, and when I test my pom.xml file, it builds successfully. Here's my pom.xml just in case:
<?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>
<groupId>Tests</groupId>
<artifactId>Test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Test1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.renjin</groupId>
<artifactId>renjin-script-engine</artifactId>
<version>3.5-beta76</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>ggplot2</artifactId>
<version>3.2.0-b8</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>dplyr</artifactId>
<version>0.8.2-b6</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>feather</artifactId>
<version>0.3.1-b9</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>hms</artifactId>
<version>0.4.2-b18</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>stringr</artifactId>
<version>1.4.0-b6</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>lubridate</artifactId>
<version>1.7.4-b4</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>forcats</artifactId>
<version>0.3.0-b9</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>haven</artifactId>
<version>2.0.0-b1</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>httr</artifactId>
<version>1.4.0-b1</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>jsonlite</artifactId>
<version>1.6-b1</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>readxl</artifactId>
<version>1.3.1-b11</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>rvest</artifactId>
<version>0.3.2-b115</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>xml2</artifactId>
<version>1.2-renjin-8</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>modelr</artifactId>
<version>0.1.1-b31</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>broom</artifactId>
<version>0.4.4-b1</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>tidyr</artifactId>
<version>0.8.0-b1</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>readr</artifactId>
<version>1.3.1-b3</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>purrr</artifactId>
<version>0.3.2-b11</version>
</dependency>
<dependency>
<groupId>org.renjin.cran</groupId>
<artifactId>tibble</artifactId>
<version>2.1.3-b9</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>bedatadriven</id>
<name>bedatadriven public repo</name>
<url>https://nexus.bedatadriven.com/content/groups/public/</url>
</repository>
</repositories>
</project>
However, when I try to run my App.java program with the R for Data Science code, it gives me loads of errors. Here is my App.java:
package Tests.Test1;
import javax.script.*;
import org.renjin.script.*;
import java.awt.*;
import javax.swing.*;
// ... add additional imports here ...
public class App {
public static void main(String[] args) throws Exception {
// create a script engine manager:
RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
// create a Renjin engine:
ScriptEngine engine = factory.getScriptEngine();
// ... put your Java code here ...
engine.eval("library(dplyr)");
engine.eval("library(ggplot2)");
engine.eval("library(tidyr)");
engine.eval("library(readr)");
engine.eval("library(purrr)");
engine.eval("library(tibble)");
engine.eval("?mpg");
engine.eval("ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))");
}
}
And here's the long list of errors I'm getting:
EEK! colSums.computeMeans() called through getElementAsDouble()
EEK! colSums.computeMeans() called through getElementAsDouble()
EEK! colSums.computeMeans() called through getElementAsDouble()
EEK! colSums.computeMeans() called through getElementAsDouble()
org.renjin.eval.EvalException: Exception initializing compiled GNU R library class org.renjin.cran.dplyr.dplyr
at org.renjin.primitives.packaging.DllInfo.initialize(DllInfo.java:141)
at org.renjin.primitives.packaging.Namespace.loadDynamicLibrary(Namespace.java:383)
at org.renjin.primitives.packaging.Namespace.importDynamicLibrary(Namespace.java:296)
at org.renjin.primitives.packaging.Namespace.initImports(Namespace.java:274)
at org.renjin.primitives.packaging.NamespaceRegistry.load(NamespaceRegistry.java:175)
at org.renjin.primitives.packaging.NamespaceRegistry.getNamespace(NamespaceRegistry.java:143)
at org.renjin.primitives.packaging.NamespaceRegistry.getNamespace(NamespaceRegistry.java:114)
at org.renjin.primitives.packaging.Packages.library(Packages.java:39)
at org.renjin.primitives.R$primitive$library.doApply(R$primitive$library.java:68)
at org.renjin.primitives.R$primitive$library.applyPromised(R$primitive$library.java:33)
at org.renjin.sexp.BuiltinFunction.apply(BuiltinFunction.java:100)
at org.renjin.primitives.special.InternalFunction.apply(InternalFunction.java:46)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.primitives.special.BeginFunction.apply(BeginFunction.java:39)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.sexp.Closure.applyPromised(Closure.java:200)
at org.renjin.sexp.Closure.apply(Closure.java:133)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.sexp.ExpressionVector.eval(ExpressionVector.java:85)
at org.renjin.eval.Context.evaluate(Context.java:280)
at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:174)
at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:133)
at Tests.Test1.App.main(App.java:19)
Caused by: java.lang.ArithmeticException: / by zero
at java.base/java.lang.Integer.remainderUnsigned(Integer.java:1564)
at org.renjin.cran.dplyr.hybrid__._ZN5boost9unordered6detail12prime_policyIjE9to_bucketEjj(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZNK5boost9unordered6detail5tableINS1_3mapISaISt4pairIKP7SEXPRECN5dplyr6hybrid15hybrid_functionEEES6_SA_NS_4hashIS6_EESt8equal_toIS6_EEEE14hash_to_bucketEj(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZNK5boost9unordered6detail5tableINS1_3mapISaISt4pairIKP7SEXPRECN5dplyr6hybrid15hybrid_functionEEES6_SA_NS_4hashIS6_EESt8equal_toIS6_EEEE14find_node_implIS6_SG_EEPNS1_8ptr_nodeISB_EEjRKT_RKT0_(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZNK5boost9unordered6detail5tableINS1_3mapISaISt4pairIKP7SEXPRECN5dplyr6hybrid15hybrid_functionEEES6_SA_NS_4hashIS6_EESt8equal_toIS6_EEEE9find_nodeEjRS7_(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZN5boost9unordered6detail5tableINS1_3mapISaISt4pairIKP7SEXPRECN5dplyr6hybrid15hybrid_functionEEES6_SA_NS_4hashIS6_EESt8equal_toIS6_EEEE14emplace_uniqueINS1_13emplace_args1ISB_EEEES4_INS0_15iterator_detail8iteratorINS1_8ptr_nodeISB_EEEEbERS7_RKT_(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZN5boost9unordered13unordered_mapIP7SEXPRECN5dplyr6hybrid15hybrid_functionENS_4hashIS3_EESt8equal_toIS3_ESaISt4pairIKS3_S6_EEE7emplaceISD_EESB_INS0_15iterator_detail8iteratorINS0_6detail8ptr_nodeISD_EEEEbERKT_(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZN5boost9unordered13unordered_mapIP7SEXPRECN5dplyr6hybrid15hybrid_functionENS_4hashIS3_EESt8equal_toIS3_ESaISt4pairIKS3_S6_EEE6insertERKSD_(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZN5dplyr6hybrid11hybrid_initEP7SEXPRECS2_S2_NS0_9hybrid_idE(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._ZN5dplyr6hybrid4initEv(hybrid.cpp)
at org.renjin.cran.dplyr.hybrid__._Z22init_hybrid_inline_mapP8_DllInfo(hybrid.cpp)
at org.renjin.cran.dplyr.RcppExports__.R_init_dplyr(RcppExports.cpp)
at org.renjin.cran.dplyr.dplyr.R_init_dplyr(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.renjin.primitives.packaging.DllInfo.initialize(DllInfo.java:137)
... 22 more
org.renjin.eval.EvalException: Exception initializing compiled GNU R library class org.renjin.cran.tidyr.tidyr
at org.renjin.primitives.packaging.DllInfo.initialize(DllInfo.java:141)
at org.renjin.primitives.packaging.Namespace.loadDynamicLibrary(Namespace.java:383)
at org.renjin.primitives.packaging.Namespace.importDynamicLibrary(Namespace.java:296)
at org.renjin.primitives.packaging.Namespace.initImports(Namespace.java:274)
at org.renjin.primitives.packaging.NamespaceRegistry.load(NamespaceRegistry.java:175)
at org.renjin.primitives.packaging.NamespaceRegistry.getNamespace(NamespaceRegistry.java:143)
at org.renjin.primitives.packaging.NamespaceRegistry.getNamespace(NamespaceRegistry.java:114)
at org.renjin.primitives.packaging.Packages.library(Packages.java:39)
at org.renjin.primitives.R$primitive$library.doApply(R$primitive$library.java:68)
at org.renjin.primitives.R$primitive$library.applyPromised(R$primitive$library.java:33)
at org.renjin.sexp.BuiltinFunction.apply(BuiltinFunction.java:100)
at org.renjin.primitives.special.InternalFunction.apply(InternalFunction.java:46)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.primitives.special.BeginFunction.apply(BeginFunction.java:39)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.sexp.Closure.applyPromised(Closure.java:200)
at org.renjin.sexp.Closure.apply(Closure.java:133)
at org.renjin.sexp.FunctionCall.eval(FunctionCall.java:80)
at org.renjin.sexp.ExpressionVector.eval(ExpressionVector.java:85)
at org.renjin.eval.Context.evaluate(Context.java:280)
at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:174)
at org.renjin.script.RenjinScriptEngine.eval(RenjinScriptEngine.java:133)
at Tests.Test1.App.main(App.java:21)
Caused by: java.lang.NoSuchFieldError: _ZTVN10__cxxabiv120__si_class_type_infoE
at org.renjin.cran.tidyr.RcppExports__._ZTIN4Rcpp9exceptionE$$clinit(RcppExports.cpp)
at org.renjin.cran.tidyr.RcppExports__.<clinit>(RcppExports.cpp)
at org.renjin.cran.tidyr.tidyr.R_init_tidyr(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.renjin.primitives.packaging.DllInfo.initialize(DllInfo.java:137)
... 22 more
I'm not quite sure how to get the code to work. If you have any ideas on how to fix this, I'd love to hear them. Please keep in mind that I am a beginner though, and I may need things in simpler/more thorough terms. If you need any more information, let me know, and I'll respond as soon as possible. Any and all help is appreciated. Thank you.
You are my last hope. I wrote little bot for telegram. And it's works perfect but i cant build it as an executable jar file. Each time i try to execute jar file at my server i get error:
Error: Could not find or load main class com.praetorian19.MainIni
I understood that i mentioned class path in wrong way. I try mention it in different ways but it's doesn't matter. Error is the same. Please look my MVN project xml and several screenshots from Idea project structure. I think i have an error in pom or in structure.
I try to wrote classpath in many ways. But result the same
I try to build jar at both platforms - Win/Lin, the same
Screenshot from Idea:
Pom:
<modelVersion>4.0.0</modelVersion>
<groupId>praetorian19</groupId>
<artifactId>praetorian19</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>4.4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jcl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.praetorian19.MainInit
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Error: Could not find or load main class com.praetorian19.MainInit
while i try to execute jar at server
Also i try
praetorian19.MainInit
But result the same
The problem is you have defined the main class as com.praetorian19.MainInit in maven-assembly-plugin, which mean the MainInit class supposed to be in the package com.praetorian19.
However when looking into IDEA project structure, your MainInit class in default package, thus it gives error Could not find or load main class com.praetorian19.MainIni, when executing the jar.
To solve this, create a package com.praetorian19 and move your MainInit class into that package with the package declaration as package com.praetorian19;
My supervisor asked me to convert an old Maven project we have lying around into a Spring Boot project such that we are able to access the project's backend via RESTful interaction (before that the project's backend was only accessible via a console interface).
So, first I added a simple Spring Boot application in a separate package of project. After that I began to extend the pom.xml of the project by the dependencies needed for Spring Boot and adjusted the overall project setup. Now, I tried to run the backend of the old project, which turned out to be working. However, the simple Spring Boot application did not.
I narrowed down the problem to a conflicting dependency in the "old" part of the pom.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.1</version>
</dependency>
When I leave this dependency in the pom.xml the old backend works, but the Spring Boot application fails with the following error:
WARN: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
If I comment this dependency out the Spring Application works completely fine, but the old backend fails. I use the version 2.0.4.RELEASE of spring-boot-admin-starter-server. I think that the old backend's version of the logging package is different from the one included in spring-boot-admin-starter-server. However, I somehow need both versions in my project.
What's not possible:
Updating the old sources, since some of them have a coyright of an
external company
What I already tried, but I wasn't successful with:
Exclude the logging from then Spring Boot depedencies. This results in the following error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
I also to tried to work with the shade plugin as some suggested from my web research. Unfortunately, I was not able to solve the problem with this approach.
Does anyone have suggestions how to solve this problem? I would be very grateful. I am not used to solve dependency problems of this kind. Please excuse me, if I am missing something obvious.
-lema
EDIT pom.xml (unfortunately I had to leave out bigger parts of it) :
<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>
...
<packaging>jar</packaging>
<description></description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.0.2</spring-boot-admin.version>
<spring-boot-dependencies.version>2.0.4.RELEASE</spring-boot-dependencies.version>
...
<rat.skip>true</rat.skip>
<log4j-version>2.6.1</log4j-version>
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
<dependencies>
<!-- Necessary dependency for running Spring Boot without starter parent -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
<!-- TODO The version of this dependency lets Spring Boot fail, but is
necessary tu run the old backend -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-iostreams</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
...
</dependencies>
<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
...
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>prepare-config-zip</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/assembly/config.xml</descriptor>
</descriptors>
<finalName>configs</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>prepare-dist-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>src/main/assembly/dist.xml</descriptor>
<finalName>...</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules>
<requireJavaVersion>
<version>1.8</version>
</requireJavaVersion>
</rules>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>attach-standalone</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>dont-attach-standalone</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<!-- Prevent huge shaded artifacts from being deployed to Artifactory -->
<outputFile>...</outputFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
EDIT: I just found out that if you remove the version element inside of the conflicting dependency the Spring Boot Application works, but unfortunately the backend then fails.
So I found a solution, but that's probably not the best way to do it:
I just replaced the <spring-boot-dependencies.version>2.0.4</spring-boot-dependencies.version> with an older version that is compatible with the conflicting logging dependency, namely version 1.4.7.RELEASE.
This is the latest version at which both the Spring Boot application and the backend are working simultaneously (found that out by try-and-error).
Anyway, thank you very much for your help.
Cheers
I am trying to create executable JAR file for my spring boot application.
To achieve this spring-boot-maven-plugin has been used with main class specified and packaging to jar.
Unfortunately after running output JAR file i receive java.lang.ClassNotFoundException pointing to my main class.
Below is my pom.xml and main class. Thanks in advance for help.
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>
<groupId>com.test</groupId>
<artifactId>interflight</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>interflight</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<feign-gson.version>7.2.1</feign-gson.version>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
<gson.version>2.8.0</gson.version>
<assertj-core.version>3.8.0</assertj-core.version>
<startClass>com.test.interflight.configuration.InterflightApplication</startClass>
</properties>
<dependencies>
<!--External dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>${feign-gson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Test dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<layout>ZIP</layout>
<mainClass>com.test.interflight.configuration.InterflightApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Output error:
$ java -jar interflight.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.test.interfl
ight.configuration.InterflightApplication
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(Laun
chedURLClassLoader.java:94)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLau
ncher.java:587)
Main class:
#SpringBootApplication
#ComponentScan(basePackages = {"com.test.interflight.controller", "com.test.interflight.implementation.*"})
#EnableFeignClients(basePackages = {"com.test.interflight.api.restclient"},
defaultConfiguration = FeignConfiguration.class)
public class InterflightApplication {
public static void main(String[] args) {
SpringApplication.run(InterflightApplication.class, args);
}
}
Short answer:
Looks like you're using the ZIP layout when you should be using the JAR layout. Change <layout>ZIP</layout> to <layout>JAR</layout> in your Spring Boot maven plugin declaration.
Longer answer:
Take a look at the docs which describe the ZIP layout as:
ZIP (alias to DIR): similar to the JAR layout using
PropertiesLauncher.
And of which the PropertiesLauncher is described in the docs as:
Launcher for archives with user-configured classpath and main class
via a properties file...
Looks in various places for a properties file to extract loader settings, defaulting to application.properties... No default, but will fall back to looking for a Start-Class in a MANIFEST.MF
If you want it to be user specified in your Jar file, specify (or check and see if you have one already) in the meta-inf folder of your Jar, or specify the property yourself.
After several days dealing with various plugins, finally I could make a fat jar (spring boot) with externalize config files everywhere I wanted to be. You can use the following plugins:"config" is the path for your config files next to the jar file.
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>jarfilename</finalName>
<mainClass>com.start.YourStarter</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/log4j.xml</exclude>
<exclude>deploy/**</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/*.txt</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.start.YourStarter</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
I want to move properties like version and other properties to pom.properties file.
I have already tried with properties-maven-plugin but still not getting any success.Please let me know how can i achieve this.
<build>
<plugins>
<!-- Maven clean plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>pom.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>....
Here is my pom.properties file data..
appserver.home=D:\apache-tomcat-7.0.64
central=http://192.168.0.110:9999/repository/internal/
snapshot=http://192.168.0.110:9999/repository/stablesnapshots/
spring.version=4.0.5.RELEASE
hibernate.version=4.3.5.Final
log4j.version=1.2.17
jdk.version=1.7
context.path=Evoke
cxf.rt.frontend.jaxrs.version=3.0.0
cxf.bundle.version=2.7.10
cxf.bundle.minimal.version=2.7.10
javax.ws.rs.api.version=2.0-m10
commons.httpclient.version=3.1
jackson.version=2.0.1
jersey.multipart.version=1.18
spring.security.version=3.2.7.RELEASE
drools.version=6.2.0.Final
itext.version=4.2.0
quartz.version=2.2.1
Have you tried this?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
<goal>write-project-properties</goal>
</goals>
<configuration>
<files>
<file>pom.properties</file>
</files>
<outputFile>out.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
It should do the trick. Goals are executed in the order they are declared.
Update based on comments
The problem with having an external file to hold the versions of your dependencies is that when you install or deploy your artifact, the POM will look like this in repository, if it was possible:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
When someone else tries to depend on this POM, how does he or she know what ${spring.version} resolves to? The answer is in the external file, which is embedded - if we are lucky - in the artifact (jar file). And to find the artifact in the repository, you need to know ${spring.version} so it becomes a catch-22 problem.
Now, having said this, Maven has its own mechanism for doing this:
<dependencyManagement> and <properties>:
DependencyManagement
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
</dependencies>
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!-- No need to specify version, it is inferred from the section above -->
</dependency>
</dependencies>
Properties
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring.version can be reused, because Maven properties behave like constants -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
The good thing is, if you you declare your versions in the <dependencyManagement> section, you can create a POM with only these type of entries in it, and have other POMs import the versions from that POM. This webpage describes how it works: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies
Since POMs always should be version controlled, you get all the added benefits from it: traceability, dependency control, portable builds, etc.