I am using the following dependecy in my pom.xml from maven:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.55</version>
</dependency>
After running the project inside eclipse everything is working fine.
By using the export feature from eclipse, creating a runnable jar file, including all dependencies also everything works fine.
No other parameters or configurations in eclipse are set.
Only when i am running a mvn built something seems to be wrong.
(Maven built runs without any error or warnings)
--> Dependencies are all included.
An error occurs in that moment when it tries to establish an https connection, the place where this bouncycastle library is used.
Error:
javax.net.ssl.SSLException: java.security.ProviderException: Could not derive key
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.SSLSocketImpl.handleException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:553)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:412)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:328)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at plugin.KITIlias.executePost(KITIlias.java:117)
at plugin.KITIlias.login(KITIlias.java:45)
at control.IliasManager.login(IliasManager.java:27)
at control.IliasStarter.login(IliasStarter.java:31)
at control.LoginProvider$1.run(LoginProvider.java:56)
at java.lang.Thread.run(Unknown Source)
Caused by: java.security.ProviderException: Could not derive key
at sun.security.ec.ECDHKeyAgreement.engineGenerateSecret(ECDHKeyAgreement.java:133)
at sun.security.ec.ECDHKeyAgreement.engineGenerateSecret(ECDHKeyAgreement.java:163)
at javax.crypto.KeyAgreement.generateSecret(KeyAgreement.java:648)
at sun.security.ssl.ECDHCrypt.getAgreedSecret(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
... 16 more
Caused by: java.security.InvalidAlgorithmParameterException
at sun.security.ec.ECDHKeyAgreement.deriveKey(Native Method)
at sun.security.ec.ECDHKeyAgreement.engineGenerateSecret(ECDHKeyAgreement.java:130)
... 25 more
Exception in thread "Thread-4" java.lang.NullPointerException
at plugin.KITIlias.executePost(KITIlias.java:125)
at plugin.KITIlias.login(KITIlias.java:45)
at control.IliasManager.login(IliasManager.java:27)
at control.IliasStarter.login(IliasStarter.java:31)
at control.LoginProvider$1.run(LoginProvider.java:56)
at java.lang.Thread.run(Unknown Source)
So for me it seems like something is missing during the maven built process.
Is it required to sign one of the jars?
my complete pom.xml is
<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>X</groupId>
<artifactId>Y</artifactId>
<version>v1.1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.20.8</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.55</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>view.Dashboard</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am getting the runnable jar with:
mvn clean install
EDIT:
Manifest file from eclipse export
Manifest-Version: 1.0
Rsrc-Class-Path: ./ junit-4.10.jar hamcrest-core-1.1.jar log4j-1.2.17.
jar jsoup-1.7.2.jar httpcore-4.4.5.jar httpclient-4.5.2.jar commons-c
odec-1.9.jar commons-logging-1.1.1.jar controlsfx-8.20.8.jar openjfx-
dialogs-1.0.2.jar bcprov-jdk15on-1.55.jar
Class-Path: .
Rsrc-Main-Class: view.Dashboard
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
-> seems like there are still dependencies to eclipse
Problem seems to be here that the dependency from bouncycastle must be signed. The jar is not signed anymore if maven repacks it inside another jar.
Solution is to keep all dependecies next to the jar file and add a classpathdirectory. Now the main jar can use directly the signed jar.
(However i would really prefer to have one single jar file)
Related
Hi I want run my project outside intelij. I build my project by means of maven. I get information that builded succes. However when i try run my jar file view error. My jdk version is 11, to project it is attached javafx library. Previously, I had version 1.8 but for sure I compile with use jdk 11. Project using also database MySql. Project in Intelij works without problems.
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/controllerCP/Main has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
<?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>ProjectCPApril</groupId>
<artifactId>ProjectCPApril</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<javafx.version>11.0.1</javafx.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- Attach the shade into the package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.controllerCP.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Hi I have downloaded jdk 8 and jdk 11 on my computer. In cmd windows I write java -version and show me 1.8 but I checked and I have in environment variables set Path to bin jdk 11.
Here is the situation.
I am developing a GUI Client for (historically) CLI based processes. These processes are each under a Maven Project in Eclipse. My GUI is a separate Eclipse Maven Project. The GUI Project references the other projects in its pom.xml, the mvn package doesn't fail, yet when I run the project's ...-jar-with-dependencies.jar I get a NoClassDefFoundError for a child's project class.
Here is my pom.xml
<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>
<artifactId>proto-xml-validator</artifactId>
<organization>
<name>SGcib</name>
<url>https://cib.societegenerale.com/en/</url>
</organization>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.miglayout/miglayout-swing -->
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-swing</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>idea</groupId>
<artifactId>ideaToolsUtils</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>bacardi</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>pnl-loader</artifactId>
</dependency>
</dependencies>
<groupId>bacardi</groupId>
<version>0.0.1-SNAPSHOT</version>
</project>
And here is the stack trace
$ java -jar target/proto-xml-validator-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: idea/file/loader/runner/ValuationBVLoaderRunner
at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.initialize(ProtoXMLValidatorGui.java:55)
at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.<init>(ProtoXMLValidatorGui.java:33)
at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui$1.run(ProtoXMLValidatorGui.java:127)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: idea.file.loader.runner.ValuationBVLoaderRunner
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)
The class that triggers the NoClassDefFoundError is located under the pnl-loader artifactId in the bacardi groupId.
I have already set the scope to compile for the Project dependencies.
Does anyone know what I might be doing wrong ?
Thanks in advance.
Solution to my problem:
The thing was that, although my Eclipse Maven Project had the correct dependency references, it couldn't properly package them in the JAR. Indeed, a project dependency is of no use to Maven (which doesn't know what an Eclipse project is, ofc). Maven goes looking for compiled JARs in his repositories. That's what #aka-one pointed out in the comments. I had to mvn clean install the several projects that were necessary to build in order to finally package my project.
I have a multimodule maven project and I like to execute a java class during the build of the multimodule.
The Java class is part of a submodule, so I tried to add the exec-maven-plugin, but it always fail with a ClassNotFoundException
The part looks like
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>build-dump</id>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>org.sonar.plugins.coffeelint2java.Coffeelint2Java</mainClass>
<arguments>
<argument>${rules.path}</argument>
</arguments>
</configuration>
</plugin>
This results in
Caused by: java.lang.NoClassDefFoundError: org/sonar/check/Priority
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter.getPriority(CheckTemplateWriter.java:126)
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter.checkLevelLine(CheckTemplateWriter.java:107)
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter.lambda$generateReplacementMapping$3(CheckTemplateWriter.java:86)
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter$$Lambda$5/161691919.accept(Unknown Source)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter.generateReplacementMapping(CheckTemplateWriter.java:84)
at org.sonar.plugins.coffeelint2java.CheckTemplateWriter.generate(CheckTemplateWriter.java:52)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java.doGenerateChecks(Coffeelint2Java.java:72)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java.generateChecks(Coffeelint2Java.java:59)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java.lambda$generateChecks$1(Coffeelint2Java.java:51)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java$$Lambda$4/1333998550.accept(Unknown Source)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java.generateChecks(Coffeelint2Java.java:51)
at org.sonar.plugins.coffeelint2java.Coffeelint2Java.main(Coffeelint2Java.java:41)
... 6 more
The sonar dependency is defined in the parent pom, but also when I add it to the <dependency> section in the submodule pom, it returns the same result.
How do I need to define the pom correctly, to include the dependencies when executing the main class ?
=== UPDATE ===
The dependency in the parent pom is
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.sslr-squid-bridge</groupId>
<artifactId>sslr-squid-bridge</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
The submodule itself also adds other dependencies
<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
</dependencies>
And right after updating my question and adding the dependency information to it, I saw it myself.
the provided attribute was the problem. argh
removing it solved the problem
a Project of mine using Commons Chain works fine, when i run it in the IDE.
Now i tried to package it with maven.
I used the following pom.xml:
<build>
<finalName>NAME</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Main</mainClass>
</manifest>
</archive>
</configuration>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
As you can see i added the Main Class to my manifest and specified the Dependencies to use.
The package process runs throught, but when i start the jar it says:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ch
ain/impl/ChainBase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at main.Main.main(Main.java:24)
Where does this error come from/what do i need to change in my config file?
Greetings,
Nicolas
You need to specify a classpath that has the jar files for your dependencies, something like java -cp=libs -jar main.jar where libs contains the dependency jar files. Maven will set that up for you with the maven-exec-plugin: mvn exec:java -DmainClass=main.Main. Maybe what you want is to have Maven create an "uber jar" that includes all of the class files for your dependencies so that java -jar main.jar just works. Use the maven-assembly-plugin or the maven-shade-plugin to create one. See What is an uber jar?.
Maven doesn't add the classpath to a jar by default.
You need to tell maven-jar-plugin to do that by adding an addClasspath entry to its configuration/archive block.
<addClasspath>true</addClasspath>
(this should be immediately before or after the mainClass line)
I have a strange problem with my Spring application. I use Eclipse and when i click RUn as Java Application my App works like a charm, but when i click maven build (mvn install) after packing to jar and try to run i get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 6 more
I gues that the problem is in this line:
public static void main(String[] args) {
ModernSoft ms = new ModernSoft();
StringBuffer classpath = new StringBuffer();
ClassLoader applicationClassLoader = ms.getClass().getClassLoader();
if (applicationClassLoader == null) {
applicationClassLoader = ClassLoader.getSystemClassLoader();
}
URL[] urls = ((URLClassLoader)applicationClassLoader).getURLs();
for(int i=0; i < urls.length; i++) {
classpath.append(urls[i].getFile()).append("\r\n");
}
System.out.println(classpath.toString());
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml");
//org.springframework.context.ApplicationContext ctx = new FileSystemXmlApplicationContext("C:\\Documents and Settings\\BK\\workspace\\ModernSoft\\src\\main\\resources\\applicationContext.xml");
DataSource ds = (DataSource) ctx.getBean("dataSource", DataSource.class);
String[] beans = ctx.getBeanDefinitionNames();
I have all jar in maven dependencies, but still i cant solve this wired problem.
Please help me.
UPDATE:
My pom.xml
<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>ModernSoft</groupId>
<artifactId>pl.modern</artifactId>
<version>0.0.1</version>
<name>ModernSoft</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<finalName>ModernSoft</finalName>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.0.Final</version>
</dependency>
</dependencies>
</project>
UPDATE
Uberjar works like a charm
Now i have another problem, after run jar file i get:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to lo
cate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa]
Offending resource: class path resource [applicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:318)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1435)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentR
eader.java:185)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocu
mentReader.java:139)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocume
ntReader.java:108)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationCo
ntext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at pl.modern.ModernSoft.main(ModernSoft.java:27)
And also i cant add or change entities.
After make customerDao.save(Customer) i get only in hibernate
Hibernate: select max(id) from customers
But no record in DB shows up.
I believe that you are running jar file from command line using java -jar In this case you will need to specify all jars which needs to be included in classpath using -classpath option.
Another option would be using Maven uber jar (jar which contains all depencies.)
The various spring jars each contain their own spring.handlers and spring.schemas files named identically, meaning the shader/uberjar plugins will pick one of them and other namespaces will fail to work.
See Invalid or corrupt JAR File built by Maven shade plugin for a shader plugin configuration merging the otherwise conflicting files. That example does have the configuration element in a weird location, my version is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<!-- must be SURE to do this with both spring.handlers and spring.schemas.
otherwise you won't be able to use them in the spring config files. -->
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</plugin>