I have a Java class which is located in src/java
that references classes from an external JAR. I copied the JAR file to the lib
directory. When I go to run the app using grails run-app
I get an error that it can't find the imported class.
What is the correct location to put JARs for a Grails project? Is there some special command to get Grails to recognize the newly added JAR files?
I am running Grails 2.0.0 with Java 1.6.0_29 on OS X.
Follow-up: The JARs are question are from Apache HttpComponents. More specifically:
commons-codec-1.4.jar
commons-logging-1.1.1.jar
httpclient-4.1.2.jar
httpclient-cache-4.1.2.jar
httpcore-4.1.2.jar
httpmime-4.1.2.jar
The class files in them are compiled to Java 1.3, which shouldn't be a problem.
~/temp/org/apache/http $ file HttpResponse.class
HttpResponse.class: compiled Java class data, version 47.0 (Java 1.3)
Very odd, I ran grails clean
and then grails run-app
and it compiled and ran fine.
When you add or remove jar files in lib
you need to run
grails compile --refresh-dependencies
in order to get Grails to notice the changes.
The correct way to add jar's is by adding it to your "BuildConfig.groovy". You can specify if it is needed at compile time, runtime etc. For example:
dependencies {
build 'org.apache.httpcomponents:httpcore:4.1.2'
build 'org.apache.httpcomponents:httpclient:4.1.2'
runtime 'org.apache.httpcomponents:httpcore:4.1.2'
runtime 'org.apache.httpcomponents:httpclient:4.1.2'
}
Please note, you may also need to uncomment the mavenCentral() line in your Buildconfig.groovy in order for the dependencies to get resolved.
See "Dependency Resolution" section here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With