則更改前一篇文章,改使用以下程式碼,如圖就可以在Eclipse Console看到如CMD裡顯示的狀態啦!
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
public class AppLauncher {
public static void main(String[] args) throws Exception
{
/*
*若需外部傳入可用下列語法,執行時ex:java AppLauncher "cmd.exe /c c:123.bat"
*/
//String cmd = args.length == 0? "java -version" : args[0];
String[] cmd = new String[3];
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
//填入執行程式路徑
cmd[2] = "D:/SplunkSearchId.bat" ;
//-s 為curl 不顯示download進度訊息
//String cmd = "java -version";
Process process = Runtime.getRuntime().exec(cmd);
new Thread(new SubTask(process.getInputStream())).start();
new Thread(new SubTask(process.getErrorStream())).start();
process.waitFor();
//多印一空白行
System.out.println();
System.out.println("====================Application terminated=======================");
}
}
class SubTask implements Runnable {
public SubTask(InputStream istream)
{
if (istream == null){
iReader = new BufferedReader(new InputStreamReader(System.in));
}
else
iReader = new BufferedReader(new InputStreamReader(istream));
}
public void run()
{
try {
String input = iReader.readLine();
while (input != null)
{
//若需執行結果可將input傳出
System.out.println(input);
/*String html = input;
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
//String testValue = "0";
for (Element e : doc.select("sid")) {
//System.out.println(e);
System.out.println("hi: " + e.text());
//testValue = e.text();
}*/
input = iReader.readLine();
}
}
catch (IOException ioe) {
}
}
private BufferedReader iReader;
}
另外請注意一下 程式碼中有註解的地方 是 XML Parser的方式,這邊使用JSOUP jar
因為Splunk Restful API回傳的default格式是XML,其實這樣已經是一個完整的Splunk with JAVA Solution (Java+CMD+cURL+JSOUP)
沒有留言:
張貼留言