Gradle build successful but local dependencies throwing noClassDefFoundError at runtime - java

I'm writing a Java program that runs from the command line and uses a library that I wrote to parse command line arguments. Since my library isn't on maven repository, I'm using flatDir in gradle to grab the jar from a directory local/ containing the jar file. Here is my build.gradle:
apply plugin: 'java-library'
repositories {
jcenter()
mavenCentral()
flatDir(dirs: 'local')
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.1'
// https://mvnrepository.com/artifact/org.mongodb/mongodb-driver
compile group: 'org.mongodb', name: 'mongodb-driver', version: '3.4.2'
// https://mvnrepository.com/artifact/org.jsoup/jsoup
compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'
// https://mvnrepository.com/artifact/log4j/log4j
compile group: 'log4j', name: 'log4j', version: '1.2.12'
// not in maven repository
compile 'JModule:JModule:1.3.1'
}
task copyToLib(type: Copy) {
from configurations.compile
into 'dependencies'
}
build.dependsOn(copyToLib)
My code runs fine in the eclipse IDE, and runs fine from the command line when I comment out all the references to JModule (my library) and run it with all the other libraries I'm using. However, when I run it with JModule as a dependency, it produces the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/jModule/def/CommandLogic
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: com.jModule.def.CommandLogic
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
I've already tried removing references to the class com.JModule.commandLogic but this only produces the same error with other JModule classes. Just to be clear, I'm not yet exporting to a jar - I'm just running the main class from the command line using the "java" command. The jar is also in the eclipse folder "project and external dependencies" so I'm assuming this means it's in my classpath, since the other jars in there run fine in my program.
Any help would be appreciated, thanks

Related

IntelliJ Gradle fails to build new project

I have clean installed the latest version on IntelliJ and Gradle, ensuring Gradle is correctly set up in my Windows environment (I can run Gradle and build my project successfully through the command line in Windows and IntelliJ).
My issue arrises when I try to get IntelliJ to update the project when I make changes to my build.gradle file (see below). I get the following error and I cannot figure out what's gone wrong. This is a fresh project and fresh install of everything.
Error:
FAILURE: Build failed with an exception.
* Where:
Initialization script 'C:\Users\Kris\AppData\Local\Temp\ijinit.gradle' line: 41
* What went wrong:
A problem occurred evaluating initialization script.
> org/gradle/internal/impldep/com/google/common/collect/Lists
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating initialization script.
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$2.run(DefaultScriptPluginFactory.java:237)
at org.gradle.configuration.DefaultScriptTarget.addConfiguration(DefaultScriptTarget.java:74)
at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:242)
at org.gradle.configuration.BuildOperationScriptPlugin$1$1.run(BuildOperationScriptPlugin.java:69)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:394)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:92)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
...
Caused by: java.lang.NoClassDefFoundError: org/gradle/internal/impldep/com/google/common/collect/Lists
at org.jetbrains.plugins.gradle.tooling.internal.ExtraModelBuilder.<init>(ExtraModelBuilder.java:55)
at org.jetbrains.plugins.gradle.tooling.internal.ExtraModelBuilder.<init>(ExtraModelBuilder.java:49)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
* Get more help at https://help.gradle.org
BUILD FAILED in 104ms
build.gradle:
plugins {
id 'java'
id 'application'
}
sourceCompatibility = 1.8
mainClassName = 'Main'
group 'com.ritcat.cinder'
version '1.0'
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
repositories {
flatDir {
dirs 'libs'
}
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile fileTree(include: ['*.jar'], dir: 'libs')
}

gradle build access denied

Hi I'm new to gradle and java and IJ idea. I am trying to build some java project using gradle. When I run command:
C:\Users\xxx\IdeaProjects\example2>gradle build
The build fails.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
> Could not resolve all files for configuration ':testCompileClasspath'.
> Could not resolve org.testng:testng:6.14.3.
Required by:
project :
> Could not resolve org.testng:testng:6.14.3.
> Could not get resource 'https://repo.maven.apache.org/maven2/org/testng/testng/6.14.3/testng-6.14.3.pom'.
> Could not GET 'https://repo.maven.apache.org/maven2/org/testng/testng/6.14.3/testng-6.14.3.pom'.
> Received fatal alert: access_denied
Folder IdeaProjects/example2 has fully accesible for all users. When I run the command with --stacktrace option, the last "caused by" is:
... 106 more
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: access_denied
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
Could it be problem with SSL certificate? I'm using proxy to connect to the web.
This is build.gradle file I'm using
plugins {
id 'java'
id 'idea'
}
group 'jetbrains'
version '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
// https://mvnrepository.com/artifact/io.rest-assured/rest-assured
//compile group: 'io.rest-assured', name: 'rest-assured', version: '3.1.0'
//testCompile group: 'io.rest-assured', name: 'rest-assured', version: '3.1.0'
}
Problem solved, thanks to stackoverflow.com/a/22666646/2987755. Following variables/properties must be set in gradle.properties file.
systemProp.http.proxyHost=$proxy
systemProp.http.proxyPort=8080
systemProp.https.proxyHost=$proxy
systemProp.https.proxyPort=8080

Java Gradle Build: NoClassDefFoundError

My build.gradle file is as following
apply plugin: 'java' sourceCompatibility = 1.8
repositories {
mavenCentral() }
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'io.javalin:javalin:1.3.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.4'
compile 'org.slf4j:slf4j-simple:1.7.25' }
jar {
manifest {
attributes(
'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' '),
'Main-Class': 'products.ProductAPI'
)
} }
task stage {
dependsOn 'build'
dependsOn 'clean'
build.mustRunAfter clean }
Trying to build a java application with gradle and deploy it to a heroku server. I have some issues on java -jar build/libs/MyApp-0.0.1.jar as it returns the following:
Exception in thread "main" java.lang.NoClassDefFoundError:
io/javalin/Javalin at products.ProductAPI.main(ProductAPI.java:7)
Caused by: java.lang.ClassNotFoundException: io.javalin.Javalin 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)
To run your app without any NoClassDefFoundError, you should have your dependencies in the runtime class path. Creating a fat jar is the simplest solution. Change your jar section like below;
jar {
manifest {
attributes(
'Class-Path': configurations.compile.files.collect {"$it.name"}.join(' '),
'Main-Class': 'products.ProductAPI')}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
run gradle build command and jar with dependencies will be created.
Other way is to add jars to runtime class path are; with java -cp flag. (Also If your application has already a classpath folder configured, copying dependencies in this folder will add them to the classpath)

ClassNotFoundException when building with gradle and trying to use com.google.code.gson

I'm new to Java projects and gradle. I'm trying to use Google's GSON to parse a JSON string.
When I navigate to my-project-name/build/classes/main/
I get the ClassNotFoundException when I run
$ java GetGroupMeMessages
Here is the stack trace (That's what it's called, right?):
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at GetGroupMeMessages.main(GetGroupMeMessages.java:59)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
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)
... 1 more
My build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'gscc-groupme-logs',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
I'm doing everything from the command line.
I've got my files saved under my-project-name/src/main/java/
Any help is much appreciated! I am stuck.
If you want to run single java file from command line, you need to provide gson jar as classpath using -cp jar_location in your command.
If you want to run build jar:
change dependencies from compile to runtime in your configuration.
dependencies {
runtime group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
runtime group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
runtime group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Change your jar config like following:
jar {
manifest {
attributes(
'Implementation-Title': 'gscc-groupme-logs',
'Implementation-Version': version
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'GetGroupMeMessages'
)
}
}
Then after creating the jar run it using java -jar jarname command.

After updating Intelij getting Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG

After updating intelij i cannot run anymore testng.xml.
If i run my tests as a separate Class it works fine and runs. But if i run it as a suite Testng.xml i got error.
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA CE.app/Contents/bin" -Didea.testng.sm_runner -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/testng/lib/testng-plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/tools.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/testng/lib/jcommander.jar" com.intellij.rt.execution.application.AppMain org.testng.RemoteTestNGStarter -port 63142 -usedefaultlisteners false -socket63143 #w#/private/var/folders/jq/g93n6rmj0xv6ccklv2l2xgs86xx4fm/T/idea_working_dirs_testng.tmp -temp /private/var/folders/jq/g93n6rmj0xv6ccklv2l2xgs86xx4fm/T/idea_testng.tmp
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
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 org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
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)
... 18 more
Process finished with exit code 1
My Gradle configuration:
group 'ao-core-android'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
test {
useTestNG {
suites 'testng.xml'
}
}
repositories {
mavenCentral()
}
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.testng:testng:6.9.10'
compile 'io.appium:java-client:4.0.0'
compile 'org.seleniumhq.selenium:selenium-java:2.53.1'
compile 'com.android.tools.build:gradle:1.1.0'
compile fileTree(dir: 'externalLibs', include: ['*.jar'])
compile files('./lib-test-tools-1.4.0.jar')
}
It highlights the
test {
useTestNG {
suites 'testng.xml'
}
}
in build.gradle.
And gives message :
test' cannot be applied to '(groovy.lang.Closure)' less... (⌘F1)
This inspection reports assignments with incompatible types
I had the same issue 'Anton Anton' reported when I ran a particular unit test. However, this didn't happen when I ran the entire suite with mvn clean test.
Exception in thread "main" java.lang.NoclassDefFoundError: org/test/TestNG
I came across this blog post in the IntelliJ Community which did work for me.
"I got it to work by selecting "In whole project" (I had "In single
module" selected) under the Configuration tab in the TestNG "Run/Debug
Configurations."
Credits to Serge Baranov and Leonardo Hernandez.

Categories