Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LiveConnect Problems with non-API methods

1 view
Skip to first unread message

Ken McCloskey

unread,
Dec 8, 1996, 3:00:00 AM12/8/96
to

I have been unsuccessful in calling JSObject methods in my Java non-API
methods. Calls to win.eval(), etc work fine in init(), start()
and other Applet (and superclass) methods, but if I create my own method
that in turn makes calls on the JavaScript window object, calls from
JavaScript to it will throw an JSException indicating that the applet
cannot call JavaScript (MAYSCRIPT not enable). I haven't heard much
about this problem on the newsgroups or FAQs, but it seems such an
easily discovered problem that it is surprising I haven't seen it
mentioned.

Thanks for any insight,
Ken
k...@media-electronica.com

<html>
<head>
<title></title>
<script language="javascript">

var System=java.lang.System;

astr="astr";
anint=1;
abool=true;
ajso=new Array();
ajso[0]=0;
ajso[1]=1;
ajso[2]=2;

</script>
</head>

<body bgcolor=#ddddaa onLoad="getVars()">
<applet code="JavaScriptTest.class"
name="JSTest"
width=320
height=200
mayscript>
</applet>
<p>

<script language="javascript">
function getVars() {
System.out.println("javascript:getVars()");
document.JSTest.jsTests(); // <========Throws Exception.
getInt();
getStrgArray();
System.out.println(astr+"/"+anint+"/"+abool+"/"+ajso);
System.out.println("Exit javascript:getVars()");
}
function getInt() {
System.out.println("javascript:getInt()");

intVal=document.JSTest.getInt();

System.out.println("intVal="+intVal);
System.out.println("Exit javascript:getInt()");
}
function getStrgArray() {
System.out.println("javascript:getStrgArray()");
strgArray=new Array();
strgArray=document.JSTest.getStrArray();
System.out.println("strArray[0]="+strgArray[0]);
System.out.println("strArray="+strgArray);
System.out.println("Exit javascript:getStrgArray()");
}
function setInt(intFromJava) {
System.out.println("javascript:setInt()");
System.out.println("intFromJava()="+intFromJava);
System.out.println("Exit javascript:setInt()");
}
function callTest() {
System.out.println("javascript:callTest()");
System.out.println("Exit javascript:callTest()");
}
</script>
</body>
</html>


import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

import netscape.javascript.*;

public class JavaScriptTest extends Applet {
JSObject win;

public void init() {

System.out.println("init()");


win=(JSObject)JSObject.getWindow(this);

// Correctly receives win JSObject (instanceof
JSObject returns
//
true).


true)

System.out.println("win="+win);

String astr=(String)win.getMember("astr");
int
anint=(int)(((Double)win.getMember("anint")).doubleValue());
boolean
abool=((Boolean)win.getMember("abool")).booleanValue();
JSObject ajso=(JSObject)win.getMember("ajso");

System.out.println(astr+"/"+anint+"/"+abool+"/"+ajso);

// All above methods work and print correct variables.

System.out.println("Exit init()");
}


// Non API-method. A reference to the win object can be made (to
// check against null, or see if instanceof JSObject), however
// calling any of win methods fails and toss exception.
public int jsTests() {

System.out.println("jsTests()");

if (win==null)
System.out.println("Win is null.");
if (win instanceof JSObject)
System.out.println("Win is JSObject");
// Above work fine.

try {
String astr=(String)win.getMember("astr");
int
anint=(int)(((Double)win.getMember("anint")).doubleValue());
boolean
abool=((Boolean)win.getMember("abool")).booleanValue();
JSObject ajso=(JSObject)win.getMember("ajso");


System.out.println(astr+"/"+anint+"/"+abool+"/"+ajso);
}
catch (JSException e) {
System.out.println("Handling JSException()...");
e.getMessage();
e.printStackTrace();
}

// Exception is throw, indicating that MAYSCRIPT is not
enabled for
// this applet. Works fine in init(), start(), stop(),
// paint() or any other API method call..

System.out.println("Exit jsTests()");
}

public int getInt() {

return -5;
}

public String[] getStrArray() {

String[] strArray={"zero","one","two","three"};
return strArray;
}

public void start() {
}

public void stop() {
}

public void paint(Graphics g) {

g.setColor(Color.white);
g.fillRect(0,0,size().width,size().height);
g.setColor(Color.black);
g.drawRect(0,0,size().width-1,size().height-1);
}
}

Jordi Munyoz

unread,
Dec 10, 1996, 3:00:00 AM12/10/96
to kmcc...@online.disney.com

Your code works fine for me. I had made only a modification, in
the jsTest method, I can't catch nothing because the compiler gives
the following error:

Exception netscape.javascript.JSException is never thrown in the body of
the corresponding try statement.
catch (JSException e) {
^

So I not use try/catch, i.e., the method I had compiled is:

public void jsTests() {
System.out.println("jsTests()");

if (win==null) System.out.println("Win is null.");
if (win instanceof JSObject) System.out.println("Win is
JSObject");
// Above work fine.

String astr=(String)win.getMember("astr");


int
anint=(int)(((Double)win.getMember("anint")).doubleValue());
boolean abool=((Boolean)win.getMember("abool")).booleanValue();
JSObject ajso=(JSObject)win.getMember("ajso");

System.out.println(astr+"/"+anint+"/"+abool+"/"+ajso);

// Exception is throw, indicating that MAYSCRIPT is not enabled


for
// this applet. Works fine in init(), start(), stop(),
// paint() or any other API method call..

System.out.println("Exit jsTests()");
}

Also note that this method is 'void'.

This is the output of the Java Console:

init()
win=[object Window]
astr/1/true/0,1,2
Exit init()
javascript:getVars()
jsTests()
Win is JSObject
astr/1/true/0,1,2
Exit jsTests()
javascript:getInt()
intVal=-5
Exit javascript:getInt()
javascript:getStrgArray()
strArray[0]=zero
strArray=[object JavaArray]
Exit javascript:getStrgArray()
astr/1/true/0,1,2
Exit javascript:getVars()

I'm using JDK 1.0.2 and Netscape 3.01 on IRIX 5.3

-- Regards, Jordi. --

Ken McCloskey

unread,
Dec 10, 1996, 3:00:00 AM12/10/96
to

The problem turned out to be that I was using J++ to compile and then
execute the applet in Netscape. I wrote to one of the Microsoft newsgroups
to see why this was the case and will post back what I hear. J++ relaxes
its restrictions to IE when you run things from it (for example, all
applets are considered trusted), but its interaction with Netscape (or
other Java-enabled browsers) has not been documented. The people that wrote
me all wrote back to say that they had no problem with the code, which is
correct. You can compile it with J++, but must run it outside of the
development environment for it to work. I figured that out by
trial-and-error last night.

Thanks,
Ken

0 new messages