jar file in Grails project lib directory not found - grails

I have a grails project and a jar file in the lib directory of the grails project. The problem is when i run the app I get class not found errors on classes within the jar file. Why can these not be seen by the grails app?

From your last response, I believe you're missing some more required jar files. You should probably check if there are any other jars required for Esendex. Hope it helps.

The lib directory is the right place for JAR files. With only the information provided above, I've no way of guessing why it's not working for you. All I can suggest is:
Run "grails war" on the command line
Unzip the created .war file and check that the JARs are in the WEB-INF\lib directory
If you're running Grails 1.2 you might want to check that the JAR isn't excluded by the new dependency resolution DSL.

Depends on how the classes are used. Once you've added them to your lib directory, try updating the dependencies config in BuildConfig.groovy. Here's mine:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
runtime 'org.apache:commons-exec:1.1'
runtime 'org.apache:httpcore:4.1.3'
runtime 'org.apache:httpclient:4.1.2'
runtime 'org.json:json:20080701'
// runtime 'mysql:mysql-connector-java:5.1.16'
}

Related

correct location for putting the runtime jar file dependency

the dependencies specified in the build config file of my grails application is as follows:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
runtime 'mysql:mysql-connector-java:5.1.19'
runtime 'org.imgscalr:imgscalr-lib:4.2'
runtime 'net.authorize:anet-java-sdk:1.4.6'
//runtime 'net.authorize:anet-java-sdk:1.8.1'
runtime 'org.apache.httpcomponents:httpclient:4.0.1'
}
I need to upgrade a library i.e replace this line
runtime 'net.authorize:anet-java-sdk:1.4.6'
with
runtime 'net.authorize:anet-java-sdk:1.8.1'
I think i also need to put the jar file at the correct location since it is specified as a runtime dependency. I have downloaded the correct jar file i.e anet-java-sdk-1.8.1.jar. I only need to know the correct location to put this file into. Can you tell me the correct location in the grails project structure where i should put this jar file and also if there is anything else i need to do for correct upgrade of this library? I appreciate your help! Thanks!
when you put in BuildConfig.groovy the following
runtime 'net.authorize:anet-java-sdk:1.8.1'
grails will search the library in the repositories you have defined (generally just above) in the part grails.project.dependency.resolution.repositories so you do not have to do anything and if you're using a standard library, it will be found in maven central and be downloaded for you.
If you do not use a open-source library or it is not pushed to any repo you can access, you need to download the jar file and put it under the lib/ folder and grails will add this file in the classpath
You can read more about dependency resolution (be sure to change the version with the one you're using, link is for 2.5.0) and all the settings that can be changed

Grails lib directory doesn't work

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.

Grails not automatically adding jar to classpath

Grails is able to configure dependencies when you specify them in the BuildConfig.groovy file. Usually when you add it there and call grails compile --refresh-dependencies it will resolve the dependency, and download to .grails/ivy-cache/..... (in my case). However, one time, after downloading the jar files, it failed to automatically add the jar to the classpath. Does anybody have any idea on how this will happen? It has worked for me before on many other Maven repository dependencies. The specific dependency I failed to add to my Grails project classpath is http://mvnrepository.com/artifact/javax.mail/mail/1.4.7
I can just manually add the lib to the classpath, but I'd rather have dependencies resolved automatically with the BuildConfig.groovy file. I also can't manually add jars into the "Grails Dependencies" library in the classpath; they can only be outside that library.

Referencing external dependencies in GGTS by convention

How do I reference a dependency by convention within my project build path? Allow me to elaborate...
I'm using the Groovy/Grails Tool Suite (GGTS 3.0). I'm referencing the dependency as such in my BuildConfig.groovy:
dependencies {
compile 'tgt:jaxb-utils:1.0'
}
The referenced jar file is successfully pulled down from the Artifactory repo - I can find the jar file on my local file system in the Ivy cache. When I run any grails targets (grails compile, grails run-app, grails run-tests), it works fine - meaning my code that imports the classes from the jar has no problems. When I run grails war, the referenced jar file is packed into the constructed war.
However, and here is the crux of this post - the project build path does not reference this jar file by default or convention, so my java or groovy code that imports the classes from the jar file reports a unable to resolve class ... error.
One solution is to simply add the jar file to the lib folder and update the build path accordingly, or modify the build path to reference the jar file in the Ivy cache folder. However, I'd have to do this for any/all dependencies pulled down in this way. Any jars in the lib folder will get saved to the team source code repository, and that seems like wasted space since the grails commands work fine with just the dependency reference in BuildConfig.groovy.
Am I just being too idealistic (ie - difficult) here, or is there a better way to clean up the unable to resolve class ... errors in my IDE without having to manually add the dependent jar files to my lib folder and update my build path? Ideas?
Eclipse / STS / GGTS : If you have Grails plugin installed, you can do the following :
Right click on your project -> Grails Tools -> Refresh dependencies (or shortcut "Alt+G, R")

How to add a "non-mavenized" jar dependency to a grails project

I'm a grails newby. I am using grails 2.0.3 from the command line (no IDE) on windows 7.
I am just running the 'hello world' example from the grails documentation.
I have a custom jar that is not in maven that I would like to use in my project. I added it to the lib folder and did a refresh-dependencies. I modified the controller to import a class from the jar. When I did 'run-app', the compiler complained that it could not find the class.
I saw several example online where people had to add a line to their BuildConfig.groovy like this: runtime 'httpclient:httpclient:3.0.1' . But since I'm not using maven or ivy, how do I get that jar on my classpath?
The example I am referring to above is http://grails.1312388.n4.nabble.com/External-Jar-in-Grails-td4388010.html
Grails 2.0-2.2
Ok, I finally figured out what I need to do. Suppose my jar is named 'mylib.jar'. I need to rename it to mylib-1.0.jar and move it to the lib directory. Then I need to add the following to BuildConfig.groovy:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.16'
runtime 'mylib:mylib:1.0'
}
I guess that was obvious to everyone but me. Hope this helps someone else.
Grails 2.3-...
Just put a jar into the lib/. Do not modify BuildConfig.groovy (c) kaskelotti
if your jar is in lib folder and also entry in buildconfig.groovy and if you can't find jar in your build path then try to use below command.
grails compile --refresh-dependencies

Resources