日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java 调用groovy_Java调用Groovy脚本

發(fā)布時(shí)間:2025/3/12 java 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 调用groovy_Java调用Groovy脚本 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在idea下,標(biāo)準(zhǔn)的Java maven項(xiàng)目中展示如何調(diào)用Groovy腳本和方法。

maven項(xiàng)目引進(jìn)Groovy lib

File -> Project Structure -> Global Libaries 添加下載好的Groovy下lib里面的jar包

調(diào)用Groovy腳本

首先,Java調(diào)用Groovy腳本需要該Groovy腳本里面有個(gè)main方法,用于傳參

import org.apache.commons.lang.StringUtils

import org.jsoup.Jsoup

import org.jsoup.nodes.Document

static void main(String[] args) {

try {

def html = args[0]

Document document = Jsoup.parse(html)

def title = document.getElementsByTag("title").first().text()

def content = document.select("#kw").html()

def article = new Article()

article.setTitle(title)

article.setContent(content)

print(article.toString())

} catch (Exception e) {

e.printStackTrace()

}

}

這個(gè)腳本的功能是解析一個(gè)網(wǎng)頁并將解析出來的數(shù)據(jù)封裝成Article對(duì)象,該Article可以是Java類也可以是Groovy類。然后在Java程序中調(diào)用該叫不能去解析網(wǎng)頁。

import com.x.script.groovy.Article;

import groovy.lang.*;

import org.apache.commons.io.IOUtils;

import java.io.File;

import java.io.FileInputStream;

public class App {

public static void main( String[] args ) {

try {

String html = IOUtils.toString(new FileInputStream(new File("C:\\Users\\linsf\\Desktop\\baidu.html")));

Binding binding = new Binding(new String[]{html});

GroovyShell shell = new GroovyShell(binding);

shell.evaluate(new File("C:\\Users\\linsf\\Desktop\\html_parser.groovy"));

} catch (Exception e) {

e.printStackTrace();

}

}

}

調(diào)用Groovy類的方法

現(xiàn)在編寫一個(gè)Groovy類,該類的工作是解析一個(gè)網(wǎng)頁并將網(wǎng)頁內(nèi)容封裝成Article對(duì)象返回。

import org.jsoup.Jsoup

import org.jsoup.nodes.Document

class BaiduParser {

Article parse(html, count) {

print(count)

try {

Document document = Jsoup.parse(html)

def title = document.getElementsByTag("title").first().text()

def content = document.select("#kw").html()

def article = new Article()

article.setTitle(title)

article.setContent(content)

return article

} catch (Exception e) {

e.printStackTrace()

}

return null

}

}

然后在Java程序中調(diào)用該類的解析網(wǎng)頁的方法。

import com.x.script.groovy.Article;

import groovy.lang.*;

import org.apache.commons.io.IOUtils;

import java.io.File;

import java.io.FileInputStream;

public class App {

public static void main( String[] args ) {

try {

String html = IOUtils.toString(new FileInputStream(new File("C:\\Users\\linsf\\Desktop\\baidu.html")));

GroovyClassLoader loader= new GroovyClassLoader();

Class fileCreator =loader.parseClass(new File("F:\\x-demo\\src\\main\\java\\com\\x\\script\\groovy\\BaiduParser.groovy"));

GroovyObject object =(GroovyObject) fileCreator.newInstance();

System.out.println("--" + object.invokeMethod("parse", new Object[]{html, 1}));

} catch (Exception e) {

e.printStackTrace();

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java 调用groovy_Java调用Groovy脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。