0

I try to add HttpBuilder into groovy script, but can do it only manually (Alt+Ctrl+Shift+S add dependencie). But when I start script I have error in line of creating new httpbuilder instance java.lang.ClassNotFoundException: org.apache.http.client.HttpClient. I manualy add HttpClient, butClassNotFoundException: net.sf.json.JSONObject and so on. But when I add Ini library it works fine.

I also tried to use @Grab

main()
def main() {
    @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
    def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')    

And have compilation error Error:Groovyc: While compiling GroovyTests: java.lang.RuntimeException: Error grabbing Grapes -- [download failed: net.sf.json-lib#json-lib;2.3!json-lib.jar]

And net in def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org') is red and Cannot resolve a symbol 'net' error

will be glad to any help

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Duxa
  • 55
  • 1
  • 7
  • I tried to create grapeConfig.xml like here https://gist.github.com/lalyos/9366690 but it doesn't work, I have the same error – Duxa Dec 16 '19 at 04:47
  • Are you connected to the internet (cos' Grab downloads the necessary jars from the internet)? The following script works fine `def g() { @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org') } g() ` – Venkatesh-Prasad Ranganath Dec 16 '19 at 05:36
  • Yes, I have internet connection. – Duxa Dec 16 '19 at 05:39
  • Do you encounter these in your IDE/Editor (while trying to get the IDE/Editor recognize the libraries being mentioned in the "grab" statement)? Or you encounter this error while executing the script? – Venkatesh-Prasad Ranganath Dec 16 '19 at 05:43
  • I encounter this error while executing the script – Duxa Dec 16 '19 at 05:44
  • What do you see when you execute the script with "groovy -Dgroovy.grape.report.download=true your-script.groovy"? – Venkatesh-Prasad Ranganath Dec 16 '19 at 05:46
  • Don't understand what you mean. I try to execute in IDE and have problem.p How can I execute script with "groovy -Dgroovy.grape.report.download=t ? – Duxa Dec 16 '19 at 05:59
  • When you execute your-script.groovy with "groovy -Dgroovy.grape.report.download=true your-script.groovy" command, was the runtime was able successfully download all of the dependencies? Or were there errors/exceptions reported in the execution? – Venkatesh-Prasad Ranganath Dec 16 '19 at 06:06
  • I execute script using green button in IDE, where I can change system property groovy.grape.report.downloads to true? – Duxa Dec 16 '19 at 06:54
  • Since you haven't mentioned details of your IDE, I can't answer that question. That said, I suggest running the script in a terminal and checking the output. – Venkatesh-Prasad Ranganath Dec 16 '19 at 08:06
  • I can't execute in command line "Groovy" is not internal or external command executing program or batch file. Which details of IDE yoe need? – Duxa Dec 16 '19 at 08:09
  • I suggest that you install groovy binaries (e.g., via sdkman (http://sdkman.io) on Linux/Mac or via downloads from groovy-lang.org). – Venkatesh-Prasad Ranganath Dec 16 '19 at 08:12
  • I've installed groovy and run script as you told and get that: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during conversion: Error grabbing Grapes -- [unresolved dependency: org.codehaus.groovy.modules.http-builder#http-builder;0.7.1: not found] – Duxa Dec 16 '19 at 10:50
  • Please post the entire output for the script `def g() { @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' ) def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org') } g()` when executed via "groovy -Dgroovy.grape.report.download=true ". – Venkatesh-Prasad Ranganath Dec 16 '19 at 22:36
  • The entire output for the script is too large so I post :: problems summary :: :::: ERRORS in cooments to Matias Bjarland – Duxa Dec 17 '19 at 03:18

1 Answers1

1

Since you have now installed the groovy executables as per the comments, the following code:

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')

import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('https://jsonplaceholder.typicode.com')
def res  = http.get(path: '/users')

println "Number of users: ${res.size()}"

should now run and print:

─➤ groovy solution.groovy
Number of users: 10

─➤ 

(tested on Groovy Version: 2.5.8 JVM: 1.8.0_232 Vendor: AdoptOpenJDK OS: Linux)

One thing that might be disrupting the artifact resolution is if you have a custom grapeConfig.xml file. This file (if it exists) should be under <user home dir>/.groovy/grapeConfig.xml and the default text that groovy uses if no grapeConfig.xml file is present can be found here:

https://github.com/apache/groovy/blob/master/src/resources/groovy/grape/defaultGrapeConfig.xml

In addition, if you need to debug grapes downloads, you should try the following flags (again as mentioned in the comments):

─➤ groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 yourGroovyScript.groovy

which should print information on what grapes are actually doing when the resolution fails.

What does your groovy -v look like? i.e. what version of groovy and what jdk are you on?

Matias Bjarland
  • 4,124
  • 1
  • 13
  • 21
  • groovy -v Groovy Version: 2.5.8 JVM: 1.8.0_201 Vendor: Oracle Corporation OS: Windows 10 – Duxa Dec 17 '19 at 02:58
  • :: problems summary :: :::: ERRORS Server access error at url https://jcenter.bintray.com/org/codehaus/groovy/modules/http-builder/http-builder/0.7/http-builder-0.7.pom (java.net.ConnectException: Connection refused: connect) Server access error at url https://jcenter.bintray.com/org/codehaus/groovy/modules/http-builder/http-builder/0.7/http-builder-0.7.jar (java.net.ConnectException: Connection refused: connect) – Duxa Dec 17 '19 at 03:12
  • Server access error at url https://repo1.maven.org/maven2/org/codehaus/groovy/modules/http-builder/http-builder/0.7/http-builder-0.7.pom (java.net.ConnectException: Connection refused: connect) Server access error at url https://repo1.maven.org/maven2/org/codehaus/groovy/modules/http-builder/http-builder/0.7/http-builder-0.7.jar (java.net.ConnectException: Connection refused: connect) – Duxa Dec 17 '19 at 03:12
  • The entire of the groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 yourGroovyScript.groovy is too long to post here, so I post only problems summary – Duxa Dec 17 '19 at 04:16
  • Can you load those URLs in a browser? They should give you either an XML file or a jar file download. It seems like there is something in your network setup preventing you from downloading the content. I can click the links in your comments and it works. If you can not load these in a browser, then this is not a groovy problem but a networking issue. – Matias Bjarland Dec 17 '19 at 07:22
  • I click the links above and download pom.xml and jars. But when I try to start script in cmd like this groovy myScript.groovy or in IntellijIdea I have org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed and java.lang.RuntimeException: Error grabbing Grapes – Duxa Dec 17 '19 at 07:32
  • Are you connecting to the web via a proxy? If so your browser might already be set to use the proxy but for groovy/java you would need to explicitly tell the jvm to use a proxy via something like `-Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080`. Other than that, I'm running out of ideas. – Matias Bjarland Dec 17 '19 at 09:09
  • I tried to set proxy but now I have java.net.MalformedURLException: no protocol: ${user.home.url}/.m2/repository/org/codehaus/groovy/modules/http-builder/http-builder/0.7/http-builder-0.7.pom But when I add dependencies manually I don't have to set proxy – Duxa Dec 17 '19 at 09:15
  • If your network requires you to use a proxy and you can not get it to work I would read up on other stack overflow answers related to proxies like this one: https://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm. Specifically, you are getting your dependencies via HTTPS, which means you need to set `https.proxyHost` `https.proxyPort` (note the S). Or try the setting which makes java use the system proxy settings (which I assume your browser is already doing): `-Djava.net.useSystemProxies=true`. – Matias Bjarland Dec 17 '19 at 09:27