There is an another way to use the Reflection API. But this is very difficult to understand and is lengthy too. In this way you don't need to modify your CLASSPATH for compilation or even import the netscape.jsobject package. The following coding demonstrates the same :
Code:
import java.awt.event.*;
import netscape.javascript.*;
import java.lang.reflect.*;
...
// This goes in Applet Class
...
String jscmd = "window.close()";
String jsrst = null;
boolean success = false;
try {
Method getw = null, eval = null;
Object jswin = null;
Class c =
Class.forName("netscape.javascript.JSObject");
Method ms[] = c.getMethods();
for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().compareTo("getWindow") == 0)
getw = ms[i];
else if (ms[i].getName().compareTo("eval") == 0)
eval = ms[i];
}
}
Object a[] = new Object[1];
a[0] = this;
jswin = getw.invoke(c, a);
a[0] = jscmd;
Object result = eval.invoke(jswin, a);
if (result instanceof String)
jsrst = (String) result;
else
jsrst = result.toString();
success = true;
}
catch (InvocationTargetException ite) {
jsresult = "" + ite.getTargetException();
}
catch (Exception e) {
jsresult = "" + e;
}
if (success)
System.out.println("eval succeeded, result is " + jsresult);
else
System.out.println("eval failed with error " + jsresult);
Bookmarks