Reputation: 707
I'm struggling with rather simple problem, but can't find solution nowhere. The point is - recently I started to learn Gradle and decided to create my first Intellij project with this build tool. I use external libraries, as defined in build.gradle:
group 'ant.rozanski'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
classpath 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.3.0'
}
}
dependencies {
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M3'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.3.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'org.apache.commons:commons-lang3:3.4'
}
And project starts from Intellij 'Run' command just fine. But, unfortunately, when I try to run it from console I recieve this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
at model.AALJourney.setParams(AALJourney.java:40)
at model.AALJourney.doJob(AALJourney.java:24)
at Start.main(Start.java:11)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonFactory
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)
... 3 more
It looks like gradle does not put those external libs on classpath. Why? How can I fix this?
Upvotes: 0
Views: 938
Reputation: 38734
You should probably use the application
plugin of Gradle. It provides a run
task that has the classpath set up correctly and also tasks for building distributable packages with Windows and *nix start scripts that set up the classpath as needed.
Besides that, if what you have shown is your full build.gradle
, the whole buildscript
block is non-sense. You add Jackson to the classpath of the build script but you don't use it anywhere. In the buildscript block you add dependencies you need in the build script, like some Gradle plugins and so on.
Upvotes: 1
Reputation:
Assuming you are running your class or jar from the console using something like java -jar ..
(you don't say how you are running it from the console, or what you are running; you should clarify this in the Question if not), this seems right. The Run target in the IDE knows how to setup the classpath for your application.
There may be an IDE target to generate a runnable jar that contains refs to all the external dependencies. But if you have a bare jar you will have to invoke the JVM with a custom classpath. Or you can roll up a runnable jars that includes the third-parties libraries.
Gradle caches the dependency jars, so you might be able to use those paths to build the classpath.
Upvotes: 1