i have been trying for several hours to get my java program to run via commandline using "java -jar myFile.jar arg1 arg2 ...", but i ONLY get this error when running via command line.
Additionally the program has a UI interface (with Swing), which runs without problems.
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
at CSV2Excel.NewJFrame.main(NewJFrame.java:467)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Row
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
Gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'
mainClassName = 'CSV2Excel.NewJFrame'
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { $it.getName() }.join(' '),
'Main-Class': 'CSV2Excel.NewJFrame',
)
}
}
repositories {
jcenter()
}
dependencies {
testImplementation 'junit:junit:4.13'
implementation 'org.json:json:20200518'
implementation 'org.apache.poi:poi-excelant:4.1.2'
implementation 'com.opencsv:opencsv:5.3'
}
If I specify the individual jars instead of "configurations.compile.collect { $it.getName() }.join(' ')" in the Classpath, I get the error that no Main-Class attribute is stored.
I am a complete beginner when it comes to Gradle.
Thank you very much.
Edit:
i am using netbeans to build my project, which gives me a zip file with a bin and lib directory with all necessary libs in it.
Related
When I build my jar, I get ClassNotFoundException for AutoConfigurationReportLoggingInitializer.
This class is not in Spring libraries built by Gradle and also the class is not necessary for running in IDE. Is it possible to disable searching for this class? Or do you have some other solution?
EXCEPTION:
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:284)
at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:438)
... 10 more
build.gradle:
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
}
Try adding the spring-boot-starter dependency which includes spring-boot-starter-logging which has the class you are missing
I build jar file in spring framework test code with gradle.
and I run jar file "java -jar jarFile.jar"
but it can't run with some error code.
I checked Main-Class attributes in spring project's build.gradle file
And MANIFEST.FM file in jar file
this is my Main class java Code
package com.apress.springrecipes.sequence;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.apress.springrecipes.sequence.config.SequenceConfiguration;
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(SequenceConfiguration.class);
SequenceGenerator generator = context.getBean(SequenceGenerator.class);
System.out.println(generator.getSequence());
System.out.println(generator.getSequence());
}
}
and this is my build.gradle file
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
mainClassName = "com.apress.springrecipes.sequence.Main"
repositories {
mavenCentral()
}
jar {
baseName = "${rootProject.name}"
version = "0.0.1-SNAPSHOT"
manifest {
attributes "Implementation-Title": "${rootProject.name}",
"Implementation-Version": version,
"Main-Class": "${mainClassName}"
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
springVersion = '4.2.0.RELEASE'
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
dependencies {
compile "org.springframework:spring-core:${springVersion}"
compile "org.springframework:spring-context:${springVersion}"
compile "org.slf4j:slf4j-simple:1.7.25"
}
this is MANIFEST.FM file in jar file
Manifest-Version: 1.0
Implementation-Title: recipe_2_2
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: com.apress.springrecipes.shop.Main
when i run jar file i get error message
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
how can i fix it ?
the gradle 'application' plugin will output the zip file in build/distributions, so you can extract into a folder, it have bin/, lib/
you can run your program just like
bin/${rootProject.name}
please don't run your jar file in the lib directly
I'm trying to add the lib from esotericsoftware "Kryo" to my libGDX project on the Desktop and Android module. I'm using Intellij.
What I tried:
Adding the kryo-3.0 folder to the External Libraries
Adding the dependencies in build.gradle to all modules
compile "com.esotericsoftware:kryo:3.0.3"
Run the Gradle
Sync Project.
What I got after compile the Desktop module:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
Caused by: java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy
at com.projectbeta.deepdarkness.screens.MenuScreen.show(MenuScreen.java:18)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.projectbeta.deepdarkness.DeepDarkness.create(DeepDarkness.java:16)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: java.lang.ClassNotFoundException: org.objenesis.strategy.InstantiatorStrategy
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)
... 5 more
Just tested and it works, paste that into root gradle.build of your project (not in some of the module specific) and resync.
project(":core") {
apply plugin: "java"
dependencies {
......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
.......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}
I'm testing out REST using Jersey, Hibernate and embedded Jetty. Initially, I was able to start everything up using ./gradlew clean jar && java -jar build/libs/<project>.jar. However, I wanted to support JSON and read from docs that MOXy is Jersey's default JSON provider. So I added the following in my build.gradle file:
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
After which, I tried to build the jar and start up jetty but I get the following issue:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:284)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:238)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:273)
at java.util.jar.JarVerifier.update(JarVerifier.java:228)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:940)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Here's my build.gradle file if it helps.
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
// Jetty
compile 'org.eclipse.jetty:jetty-server:9.3.1.v20150714'
compile 'org.eclipse.jetty:jetty-servlet:9.3.1.v20150714'
// Jersey
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.19'
compile 'org.glassfish.jersey.core:jersey-client:2.19'
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
compile 'org.glassfish.jersey.media:jersey-media-json-processing:2.19'
compile 'org.glassfish.jersey.media:jersey-media-multipart:2.19'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.19'
// Hibernate
compile 'mysql:mysql-connector-java:5.1.36'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'com.mchange:c3p0:0.9.5.1'
testCompile 'junit:junit:4.12'
}
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}
After looking a bit more into how gradle builds the fat jar, what solved the issue was to fix jar block:
jar {
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}
I am using Java SpringBoot with Jetty and Cassandra. I followed the basic tutorial for setting up a RESTful server, but I'm getting an exception when i try to run the jar. Here is the exception:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: javax/servlet/HttpConstraintElement
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:619)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:880)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:869)
at kikpay.Application.main(Application.java:16)
... 6 more
Caused by: java.lang.NoClassDefFoundError: javax/servlet/HttpConstraintElement
at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:100)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:159)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132)
... 13 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.HttpConstraintElement
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:133)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:103)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 16 more
Here is my build.gradle:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'example'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.data:spring-data-cassandra:1.0.0.BUILD-SNAPSHOT")
compile("com.googlecode.json-simple:json-simple:1.1")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.10'
}
I am new to Spring, so any help would be greatly appreciated.