Can run in Intellij, but jar file fails - java

Instead of running Quarkus, I'm running OptaPlanner through a Main class entry point in Kotlin.
This works great within Intellij where I have a simple Run Configuration set up for it.
object Main {
lateinit var solverFactory: SolverFactory<VehicleRoutingSolution>
lateinit var solver: Solver<VehicleRoutingSolution>
lateinit var scoreManager: ScoreManager<VehicleRoutingSolution, SimpleLongScore>
#JvmStatic
fun main(args: Array<String>) {
...
However, when I create an Intellij artifact for a jar and try to run it, I get this:
java -jar acme.jar
Apr. 28, 2022 5:21:13 P.M. com.sun.xml.bind.v2.runtime.reflect.opt.Injector <clinit>
SEVERE: null
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:573)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:166)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:51)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:157)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:255)
at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:99)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:150)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:484)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:301)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:88)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:80)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.<init>(SolverConfigIO.java:27)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:213)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:188)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:128)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:103)
at org.optaplanner.core.api.solver.SolverFactory.createFromXmlResource(SolverFactory.java:55)
at org.acme.bootstrap.Main.initOptaPlanner(Main.kt:112)
at org.acme.bootstrap.Main.main(Main.kt:43)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
at java.base/java.lang.Class.getMethod(Class.java:2227)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:170)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:166)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
... 31 more
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:294)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:66)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:57)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:157)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:255)
at com.sun.xml.bind.v2.runtime.property.SingleElementLeafProperty.<init>(SingleElementLeafProperty.java:62)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:99)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:150)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:484)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:301)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:109)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1126)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:135)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:35)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:393)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:88)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.<init>(GenericJaxbIO.java:80)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.<init>(SolverConfigIO.java:27)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:213)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:188)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:128)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlResource(SolverConfig.java:103)
at org.optaplanner.core.api.solver.SolverFactory.createFromXmlResource(SolverFactory.java:55)
at org.acme.bootstrap.Main.initOptaPlanner(Main.kt:112)
at org.acme.bootstrap.Main.main(Main.kt:43)
Shutting down
In solverConfig.xml I'm using <domainAccessType>REFLECTION</domainAccessType> because I cannot get Gizmo to work. I'm not sure if that's related or not, but why is there a difference between an Intellij Run Config and the Jar artifact?
Thanks
*** UPDATE ***
I still get the above error message - even when creating a very basic OptaPlanner project.
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>foo</artifactId>
<groupId>org.acme</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.6.21</kotlin.version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<optaplanner.version>8.20.0.Final</optaplanner.version>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<finalName>foo</finalName>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.acme.bootstrap.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-bom</artifactId>
<type>pom</type>
<version>${optaplanner.version}</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.optaplanner</groupId>
<artifactId>optaplanner-core</artifactId>
</dependency>
</dependencies>
</project>
Here's what I run with Maven:
mvn clean compile package
java -jar ./target/foo-jar-with-dependencies.jar
Note: I'm using openjdk-17.0.2
java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
It still runs fine within IntelliJ's system, but from Maven it just throws the above error.
Also, here's my solverConfig.xml for reference.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<solver xmlns="https://www.optaplanner.org/xsd/solver">
<solutionClass>org.acme.domain.MySolution</solutionClass>
<entityClass>org.acme.domain.Visit</entityClass>
<scoreDirectorFactory>
<constraintProviderClass>org.acme.solver.MyConstraintProvider</constraintProviderClass>
</scoreDirectorFactory>
<termination>
<millisecondsSpentLimit>2000</millisecondsSpentLimit>
</termination>
</solver>
And my Main.kt file:
object Main {
lateinit var solverFactory: SolverFactory<MySolution>
lateinit var solver: Solver<MySolution>
lateinit var scoreManager: ScoreManager<MySolution, SimpleLongScore>
#JvmStatic
fun main(args: Array<String>) {
solverFactory = SolverFactory.createFromXmlResource("solverConfig.xml")
solver = solverFactory.buildSolver()
scoreManager = ScoreManager.create(solverFactory)
}
}

An answer to another question suggests you need an extra artifact in your project. Technically, optaplanner-core module brings it, but the scope is runtime - so that dependency may not show up in the JAR with dependencies. (?) You will need to include it yourself; you can confirm that by checking mvn dependency:tree in your project.
That said, I am not sure why we use the runtime scope here. Maybe we need to re-evaluate.
Also, seeing as you are using an uberjar, I'm going to point to another uberjar-related question to perhaps pre-empt more struggle.

Related

MongoCommandException: Command failed with error 8000 (AtlasError): 'no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.'

I'm using Java 11 (Maven project) for mongodb Free Tier Cluster (Version 4.0.13). I'm trying to connect via connection-string (for 3.6 drivers or later) like:
mongodb+srv://user:pass#cluster0-ox90k.mongodb.net/test?retryWrites=true&w=majority
and by the same way via connection-string (for 3.4 driver or later):
mongodb://user:pass#cluster0-shard-00-00-ox90k.mongodb.net:27017,cluster0-shard-00-01-ox90k.mongodb.net:27017,cluster0-shard-00-02-ox90k.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority
I've already tested different dependencies for java drivers like: mongodb-driver-sync (ver. 3.11.0), mongodb-driver-sync (ver. 3.10.0) , mongodb-driver-sync (ver. 3.8.0).
Maven dependency looks like this:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.11.0</version>
</dependency>
Also I tried to use mongo-java-driver via connection-string for 3.6 or later drivers/3.4 or later and versions like: 3.11.0, 3.10.0, 3.8.0, 3.7.0.
Maven dependency looks like this:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.0</version>
</dependency>
And I'm getting always the same issue:
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 8000: 'no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.' on server cluster0-shard-00-01-ox90k.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.", "code" : 8000, "codeName" : "AtlasError" }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164)
at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:286)
at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:247)
at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
My current POM is:
<?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>test</groupId>
<artifactId>test_project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library # https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>project.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Any ideas how to solve the issue will be appreciated.
I had the same issue using a Docker image from adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim. Since I've upgraded to adoptopenjdk/openjdk11-openj9:jdk-11.0.5.10-alpine-slim, my connection to MongoDB is OK.
Java 8
I've changed Java 11 to Java 8 and it solved the issue. Looks like Java 11 doesn't support SNI. Because all attempts were unsuccessful despite the fact that I used the latest drivers.
Java 11
Thanks to clarification by Virg, I've changed version of Java 11, but also the license. I had Java 11 using Java Development Kit builds (from Oracle) - as sourse. Then I've installed another license as Azulu using JDK version 11.0.5+10 and it also solved the error.

Maven not finding apache RandomStringGenerator

I am trying to use the new Apache Commons Text new random string generator, but I can't find any usage on the Internet on how to properly import it. Maven builds successfully, but when I try to run my jar file from the command line with the following command
java -cp target/my-app-1.0.jar com.mycompany.app.App
…I get the below error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/text/RandomStringGenerator$Builder
at com.mycompany.app.App.main(App.java:8)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.RandomStringGenerator$Builder
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
I also have this warning during the build:
The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more detail
Here is the Maven debug version:
[WARNING] The POM for org.apache.commons:commons-text:jar:1.1 is invalid, transitive dependencies (if any) will not be available: 1 problem was encountered while building the effective model
[FATAL] Non-parseable POM C:\Users\me\.m2\repository\org\apache\commons\commons-text\1.1\commons-text-1.1.pom: only whitespace content allowed before start tag and not a (position: START_DOCUMENT seen a... #1:1) # line 1, column 1
Here is my App.java code:
package com.mycompany.app;
import org.apache.commons.text.RandomStringGenerator;
public class App {
public static void main(String[] args) {
// Generates a 20 code point string, using only the letters a-z
RandomStringGenerator generator = new RandomStringGenerator.Builder()
.withinRange('a', 'z').build();
String randomLetters = generator.generate(20);
System.out.println( randomLetters );
}
}
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
If you want to run you application from a jar, you will need an exploded jar. The default class loader will not find the apache jar from your jar.
You can use the shade plugin to create an exploded jar. Add something like below to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.mycompany.app.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, you can run with the following:
java -jar target/my-app-1.0.jar

Runtime error in Codenvy Java execution

I am newbie with Codenvy and I am developing a RESTful service using Jersey based on this code. When I build and run the code, the console returns this error.
[STDERR] Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory
[STDERR] at com.apiconnect.Main.main(Main.java:22)
[STDERR] Caused by: java.lang.ClassNotFoundException: com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory
[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[STDERR] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[STDERR] at java.security.AccessController.doPrivileged(Native Method)
[STDERR] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
[STDERR] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
[STDERR] ... 1 more
Line 22 is:
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);
EDIT: pom.xml is:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.apiconnect</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>apiconnect-example</artifactId>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
<version>1.9-ea01</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9-ea01</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9-ea01</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-i</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.apiconnect.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.apiconnect.Main</mainClass>
<name>app</name>
</program>
</programs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build></project>
I have also tried my code in Netbeans and works fine. What is the problem and how can I solve it?
In Codenvy, build and run processes take place on different nodes and different environments. This project produces /repo with quite a few jars and a startup script that is impossible to inject into a Docker container (Codenvy runners are Docker based). Thus, I recommend performing build and run in the same environment, i.e. in the runtime. What you need to do is create a new runner (the button is on the runner panel), install Maven there, perform build and execute startup script. You will also have to unbind the service from localhost (see screenshot below). Your machine recipe will be the following:
FROM codenvy/jdk7
# install Maven
RUN mkdir -p /home/user/maven3 && \
wget -qO- "http://archive.apache.org/dist/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gz" | tar -zx --strip-components=1 -C /home/user/maven3
ENV M2_HOME /home/user/maven3
RUN echo "export M2_HOME=$M2_HOME" >> /home/user/.bashrc
ENV PATH $M2_HOME/bin:$PATH
RUN echo "export PATH=$PATH" >> /home/user/.bashrc
#expose port
EXPOSE 9998
#map it to an external port
ENV CODENVY_APP_PORT_9998_HTTP 9998
# add project sources
ADD jax-rs-sample-1.0-SNAPSHOT-jar-with-dependencies_sources_unpack /home/user/app/
# change permissions for project folder
RUN sudo chown -R user:user /home/user/app
# build and run
CMD cd /home/user/app && \
mvn package -q && \
sudo chmod a+x /home/user/app/target/bin/app && \
/home/user/app/target/bin/app 2>&1
This will trigger a build and run the service. Take a look at the URL and port in the bottom of a runner panel. We map an exposed port in Docker to an external port on a runner instance. Hosts and ports are randomly chosen each time you hit Run.
Your code is not finding com/sun/jersey/api/container/grizzly/GrizzlyWebContainerFactory class in your classpath. Try adding below dependency:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
<artifactId>grizzly-servlet-webserver</artifactId>
<version>1.9.18-i</version>
</dependency>

How to make fat-jar with Embedded EJB container

I'm trying to create a fat-jar using Embedded EJB container.
My problem is that when I try to run the jar file, IllegalArgumentException occurred.
Please help me.
My Code.
public static void main(String[] args) throws NamingException {
try (EJBContainer container = EJBContainer.createEJBContainer()) {
String jndi = "java:global/javaee-batch/MainLogic";
MainLogic logic = (MainLogic) container.getContext().lookup(jndi);
logic.run();
}
}
If I can execute by maven, this code is successful.
here are my poms:
<?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>simplebatch</groupId>
<artifactId>JavaEE-Batch</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>simplebatch.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-sample-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
</dependencies>
</project>
When I execute "JavaEE-Batch-1.0-SNAPSHOT-jar-with-dependencies.jar", this error occurred.
Sep 15, 2014 8:21:46 PM org.glassfish.api.ActionReport failure
SEVERE: Exception while deploying the app [javaee-batch]
Sep 15, 2014 8:21:46 PM com.sun.enterprise.v3.server.ApplicationLifecycle deploy
SEVERE: Exception during lifecycle processing
java.lang.IllegalArgumentException: Referencing error: this bundle has no bean of name: MEJBBean
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.getEjbByName(EjbBundleDescriptorImpl.java:323)
at org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl.getEjbByName(EjbBundleDescriptorImpl.java:307)
at org.glassfish.ejb.deployment.node.runtime.EjbNode.setElementValue(EjbNode.java:133)
at com.sun.enterprise.deployment.node.SaxParserHandler.endElement(SaxParserHandler.java:617)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLNSDTDValidator.endNamespaceScope(XMLNSDTDValidator.java:266)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2005)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:879)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
What do I do to
Is MEJBBean part of your beans or an external library?
The stacktrace is missing more information. There should be at least caused by... There is a likelihood that maven (on whichever ide you use) sets the correct dependency paths, but on building with dependency, this path is not copied, either because this external dependency has scope provided or is set as optional

Maven local dependency not resolving sub-dependencies

I'm having problems resolving sub-dependencies from a third-party package. I'm a bit of a Maven beginner. Basically it goes like this:
git clone git://github.com/unidata/thredds.git
cd thredds
mvn install
Everything works great and stuff is installed into ~/.m2. Now, I wrote my own code that uses the package that I just installed:
<?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>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>Test Package</name>
<dependencies>
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.8-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Ok, this also works great when I run mvn package. The netcdf artifact is found. Now, when I try to run my code, I start down a long ClassNotFoundException path, having to keep adding all of netcdf's dependencies to my classpath.
Am I doing something wrong, or should the thredds package and all its dependencies automatically be picked up?
edit: the thredds package has many sub-modules one of which is netcdf. My code only depends on the netcdf jar.
edit: the snapshot version is installed
$ ls -l ~/.m2/repository/edu/ucar/netcdf/4.3.8-SNAPSHOT/
total 4272
-rw-rw-r-- 1 nwatkins nwatkins 700 2012-03-29 23:23 maven-metadata-local.xml
-rw-rw-r-- 1 nwatkins nwatkins 182 2012-03-29 23:23 _maven.repositories
-rw-rw-r-- 1 nwatkins nwatkins 4357494 2012-03-29 23:23 netcdf-4.3.8-SNAPSHOT.jar
-rw-rw-r-- 1 nwatkins nwatkins 7840 2012-03-29 22:28 netcdf-4.3.8-SNAPSHOT.pom
edit: to run the code which is in a single file Test.java
$ java -cp target/test-0.1.jar Test
edit: first error message
$ java -cp target/test-0.1.jar Test
Exception in thread "main" java.lang.NoClassDefFoundError: ucar/ma2/InvalidRangeException
Caused by: java.lang.ClassNotFoundException: ucar.ma2.InvalidRangeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
edit: then I just try to manually add the dependency jar to the class path
$ java -cp ../thredds/cdm/target/netcdf-4.3.8-SNAPSHOT.jar:target/test-0.1.jar Test
xception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at ucar.nc2.NetcdfFile.<clinit>(NetcdfFile.java:97)
at Test.main(Test.java:37)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
The slf4j package is also in ~/.m2. I stopped trying to add things to the classpath after this as it seemed like the wrong approach.
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Two solutions here (the cause of the problem has been explained in several answers):
1.Use maven exec plugin to launch your app:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>runtime</classpathScope>
<executable>java</executable>
<commandlineArgs>-classpath %classpath Test</commandlineArgs>
</configuration>
</plugin>
2.You can also package your jar with all its dependencies, using maven assembly plugin. To do that, you need to add the following to your plugins of your build:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>build-package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Then you can use the output jar created by maven assembly in your classpath arg
It sounds like you are trying to run your project from the target folder, and it is not seeing the transitive dependencies. You will need to use the maven dependencies plugin to have those dependencies copied over along with your jar file.
See also:
http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
You can then define the dependencies on these jars either manually, or via a generated manifest file:
http://maven.apache.org/shared/maven-archiver/index.html
The maven project that you've created only packages your classes into test-0.1.jar.
If you use a simple java command to run your application, you MUST provide all other jars that your project depends on to the -classpath ( or -cp ) switch ( or through CLASSPATH environment variable ).
There are alternative packaging configurations that can copy all required dependencies into a single jar, if that is what you want.
Here is the example, which is a verbatim copy from here -> http://www.sonatype.com/books/mvnref-book/reference/assemblies-sect-basics.html. I recommend that you read that whole chapter ( heck the whole book ) for better understanding.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.assemblies</groupId>
<artifactId>executable-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Assemblies Executable Jar Example</name>
<url>http://sonatype.com/book</url>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.sonatype.mavenbook.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If you want to have all the dependencies in classpath, when running your jar, you can use the maven-exec-plugin to run the program from withing maven, and have all the dependencies automatically added to your classpath.
You are missing the dependency for:
org/slf4j/LoggerFactory
which means in other words you need to add a dependency to your project:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
Assuming you are using log4j ?
And furthermore based on the missing class: ucar.ma2.InvalidRangeException it looks you need an other supplemental dependency except (May be take a look into the documentation if exists).
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.8-SNAPSHOT</version>
</dependency>

Categories