import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
try {
FileReader reader = new FileReader("C:/yourfile.js");
engine.put("urlfromjava", "http://www.something.com/?asvb");
engine.eval(reader);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
眼下,yourfile.js包含此行執行JavaScript在Java中 - 打開一個URL,並獲取鏈接
function urlget(url)
{
print("URL:"+url);
var loc = window.open(url);
var link = document.getElementsByTagName('a')["61"].href;
return ("\nLink is: \n"+link);
}
var x = urlget(urlfromjava);
print(x);
我得到的錯誤
"javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined"
如何打開一個URL並從java獲得它的鏈接?
哇!看起來像我已經回答了相同的:) – Tapos 2011-05-22 09:54:21
「偉大的頭腦相似」 – iruediger 2011-05-22 11:40:44
我喜歡的答案,除了,w3schools是像維基百科或隨機的網頁搜索結果一樣多的「文檔」。所以這個答案的前兩行是不正確的。 – Kit10 2014-01-09 15:22:53