I have added a jar in to lib folder and refresh dependencies but there is always Caused by NoClassDefFoundError: Could not initialize class. Maybe somebody have some ideas?
You'll get a ClassNotFoundException if you try to load a class that isn't in the classpath, but NoClassDefFoundError is quite different and often tricky to resolve. This is because they occur when the class exists, but another class (or a resource in some cases) that it depends on cannot be loaded.
Try to find out what other libraries are needed by this jar file. http://mvnrepository.com/ is a great site for displaying library dependencies. Of course, if this jar is in a public Maven repo, you should delete it from the lib dir and change to using a dependency in BuildConfig.groovy. That way it will be downloaded once and cached, and reused in multiple projects, and all of its dependencies and transitive dependencies will also be downloaded and added to the classpath.
Related
I'm trying to find documentation and code samples on how to add a local / non-maven jar file to my Grails 3.x project?
I found the separate thread How to add a non-maven jar to grails - but that's only to grails 2.3, and the file structure and configuration has undergone a big overhaul in 3.x.
Any help and (especially) code samples would be wonderful! The .jar is in the local project directory, and I intend to package with the .war for deployment.
Additionally, once i add the dependency, should i just be able to call it's methods from the controller & service files? or do i need to include them in those as well?
thx!
Grails 3 uses Gradle, so there's nothing Grails specific about including a local jar. It's as easy as adding a file dependency to the dependencies block of your build.gradle file.
Per the Gradle documentation on File Dependencies:
To add some files as a dependency for a configuration, you simply pass a file collection as a dependency:
dependencies {
...
compile files('libs/a.jar', 'libs/b.jar')
// or
compile fileTree(dir: 'libs', include: '*.jar')
}
The above example shows two ways to include jars that exist in a local libs/ directory; you can do either/or. The jar(s) can be anywhere on the filesystem, just make sure you point to the correct path.
To use the classes from the dependency in your application, you'll include them in your services, controllers and all other classes like you normally would. Say libs/a.jar has a class org.example.Something, you'd add an import to the top of your Grails class like so:
import org.example.Something
I use Grails 2.2.3. I have put jar file in lib directory, IDEA immediately resolved the dependency. But when I start app I get NullPointerException on class from this library. If I try it second time or more I get java.lang.NoClassDefFoundError. I found a lot of advice how to resolve this issue but none were useful in my case.
Library (mylib-1.jar) compiled in maven and added to lib dir. In BuildConfig.groovy, dependency is mentioned as:
dependencies {
compile 'com.mylib:mylib:1'
}
I tried
grails clean
grails compile --refresh-dependencies
grails refresh-dependencies
but nothing helps. In result war file I can see this library in WEB-INF/lib, but even if deploy this war I get the same error.
How can this be resolved?
You're confusing NoClassDefFoundError with ClassNotFoundException. ClassNotFoundException happens when a class you want isn't there, but you get a NoClassDefFoundError when the class is there, but a class it depends on isn't. So you're missing another jar file that this jar file depends on.
This is one of the many reasons why it's best to use dependency management instead of manually copying jar files to the lib directory. If you use a Maven repo where the jars have proper POM files, their dependencies are specified, and the resolver can download the entire tree of dependencies for you, rather than you having to find all of the jars yourself.
I have an app in Grails that uses a .java to manage paypal MassPay feature. Like many .java, it needs some jars that enclose the classes that jar uses. Ok, i import that jars and the errors in the .java dissapears. But now, when I try to run the app, i receive 25 messages like this:
myapproute/grails-app/controllers/com/mycompany/widget/MassPay.java:3: package com.paypal.sdk.profiles does not exist
import com.paypal.sdk.profiles.APIProfile;
That file in the MassPay.java does not throw any error, since i imported the jar where that class is enclosed. But it doesn't allow me to run the project.
Any help? thanks.
Im using Eclipse, not NetBeans (i have read that there is a bug in Netbeans)
Adding JARs to the Eclipse project build path is not sufficient to make them visible to Grails. You need to either put them in the application's lib directory and run grails compile --refresh-dependencies or (better) if the JARs are available in a Maven-compatible repository simply declare your dependencies in BuildConfig.groovy and let Grails download the JARs itself.
Run this - it will work
grails clean
I have a plugin that loads a custom Ivy resolver. It uses ${basedir}
to locate the jar file containing the resolver so I can load it inside
BuildConfig (see the answer for context). That compiles the plugin, but unfortunately, when the plugin is installed in a project,basedir becomes the project directory so it can't find the jar. pluginBasedir doesn't seem to point to anything, even inside the plugin's BuildConfig.groovy.
Is there any way to figure out the plugin base directory from within a
plugin's BuildConfig.groovy?
Ultimately I just want my custom resolver (in an external jar) to work when compiling the plugin and when compiling any project the plugin is a part of. Any solution is welcome.
The best answer I could come up with was to get my jar into a public Maven repo and use #Grab. e.g.,
#Grab(group='com.noahsloan.atg',module="atg-resolver",version="1.0")
import com.noahsloan.atg.ivy.AtgModuleRepository
grails.project.dependency.resolution = {
resolver AtgModuleRepository.newResolver
From my plugin. I'd still like to know if there is a way to reference pluginBasedir from BuildConfig.
I recently upgraded a project to Grails 1.3.5. This deleted everything in the /lib dir, though the project continues to work, so I guess the way dependencies are specified (and the location they're stored) has changed. I want to remove some libs that I'm no longer using, but can't do this until I find where the dependencies are specified.
Thanks,
Don
I'm surprised that anything was deleted from your lib directory - that shouldn't happen.
Dependencies are registered in BuildConfig.groovy in your app and in the plugins that work with Grails 1.2 and above. Older plugins will continue to have jars in their lib directories which will be added to the classpath, and you can still do the same. Obviously it's best to use the dependency management if possible so you have just the one copy of the jar in your Ivy cache instead of one for every project on your machine.
You can run grails dependency-report to generate Ivy reports to see what's managed by Ivy in each environment. These will end up in target/dependency-report and there's no index file, so just open any of the .html files and you can navigate to the others from there, e.g. target/dependency-report/org.grails.internal-{appname}-runtime.html.