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

How to get the result of a javascript method in a java application?

9 views
Skip to first unread message

JCD

unread,
May 25, 2008, 5:46:42 AM5/25/08
to
Hello.
I'd like to get the result of a javascript method (executed in a web
browser) in a java application. Is it possible and how can I do it?
thank you.

Andrew Thompson

unread,
May 25, 2008, 6:43:03 AM5/25/08
to
On May 25, 7:46 pm, JCD <jcd.n...@club-internet.fr> wrote:
> Hello.
> I'd like to get the result of a javascript method (executed in a web
> browser)

In a web page? What URL?

>.. in a java application. Is it possible and how can I do it?

(One way is to) Put an applet in the web page
that is signed, and convince the end user to
trust it. The applet communicates with the
JavaScript, and gets the desired result, the
Java applet then comunicates the result to the
Java application - there are probably a number
of ways to do that, but I would look to using
Sockets.

Another option might be to run a scripting engine
within the Java application, that opens the JS
directly.

But, why not simply re-implement the JS in Java?

> thank you.

No worries.

--
Andrew T.
PhySci.org

Vivien Barousse

unread,
May 25, 2008, 6:57:35 AM5/25/08
to

Hi.

Yes it is possible. Take a look at the javax.script package (which exists
since Java 6).

Here is an example, executing a JS method called "myFunction" :

public static void main(String[] args) {
try {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String myJSCode = new StringBuffer()
.append("function myFunction() {")
.append("return (4 + 5);")
.append("}")
.append("myFunction();").toString();
System.out.println(engine.eval(myJSCode));
} catch (ScriptException ex) {
ex.printStackTrace();
}
}


Hope this helps,

Vivien Barousse

JCD

unread,
May 25, 2008, 7:52:26 AM5/25/08
to

I've created a google map in an html page wich is displayed in the web
browser. When I drag the map, I want to get the coordinates of the new
center of the map in a java application. The Javascript method is
map.getCenter() and I need the result in my application.
Do you think javax.script can help?

Arne Vajhøj

unread,
May 25, 2008, 9:33:25 AM5/25/08
to
JCD wrote:
> I'd like to get the result of a javascript method (executed in a web
> browser) in a java application. Is it possible and how can I do it?

JSObject window = JSObject.getWindow(this);
window.eval("yourjsfunc()");

Arne

Arne Vajhøj

unread,
May 25, 2008, 9:38:53 AM5/25/08
to
Vivien Barousse wrote:
> On Sun, 25 May 2008 02:46:42 -0700, JCD wrote:
>> I'd like to get the result of a javascript method (executed in a web
>> browser) in a java application. Is it possible and how can I do it?

> Yes it is possible. Take a look at the javax.script package (which exists

> since Java 6).
>
> Here is an example, executing a JS method called "myFunction" :
>
> public static void main(String[] args) {
> try {
> ScriptEngineManager mgr = new ScriptEngineManager();
> ScriptEngine engine = mgr.getEngineByName("JavaScript");
> String myJSCode = new StringBuffer()
> .append("function myFunction() {")
> .append("return (4 + 5);")
> .append("}")
> .append("myFunction();").toString();
> System.out.println(engine.eval(myJSCode));
> } catch (ScriptException ex) {
> ex.printStackTrace();
> }
> }

Ah. That is also a possibility.

I read it as calling some JS embedded in the HTML, but it
could also be embedded in (or fetched by) Java.

And since the embedded JS can use Java classes, then the forms
of JS can actually interact using Java as glue.

Arne

JCD

unread,
May 25, 2008, 9:44:57 AM5/25/08
to

This code works with an applet but not with a java application, does
it?

Arne Vajhøj

unread,
May 25, 2008, 9:46:31 AM5/25/08
to
JCD wrote:
> On 25 mai, 15:33, Arne Vajhøj <a...@vajhoej.dk> wrote:
>> JCD wrote:
>>> I'd like to get the result of a javascript method (executed in a web
>>> browser) in a java application. Is it possible and how can I do it?
>> JSObject window = JSObject.getWindow(this);
>> window.eval("yourjsfunc()");
>
> This code works with an applet but not with a java application, does
> it?

That is correct.

If it is a standalone app then you need to use the method
described by Vivien.

Arne

JCD

unread,
May 25, 2008, 10:29:26 AM5/25/08
to

Ok but with Viviens'method, I don't understand the link between the
function map.getCenter() wich is in my HTML page and his Java code.
Can you give me the exact code to evaluate map.getCenter() in my java
code, please?

Arne Vajhøj

unread,
May 25, 2008, 10:34:31 AM5/25/08
to
JCD wrote:
> On 25 mai, 15:46, Arne Vajhøj <a...@vajhoej.dk> wrote:
>> JCD wrote:
>>> On 25 mai, 15:33, Arne Vajhøj <a...@vajhoej.dk> wrote:
>>>> JCD wrote:
>>>>> I'd like to get the result of a javascript method (executed in a web
>>>>> browser) in a java application. Is it possible and how can I do it?
>>>> JSObject window = JSObject.getWindow(this);
>>>> window.eval("yourjsfunc()");
>>> This code works with an applet but not with a java application, does
>>> it?
>> That is correct.
>>
>> If it is a standalone app then you need to use the method
>> described by Vivien.
>
> Ok but with Viviens'method, I don't understand the link between the
> function map.getCenter() wich is in my HTML page and his Java code.
> Can you give me the exact code to evaluate map.getCenter() in my java
> code, please?

I think you need to start by explaining how your Java app and
a web page is supposed to interact.

Arne

JCD

unread,
May 25, 2008, 11:37:54 AM5/25/08
to
> Arne- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

well, on the host computer, there is an html page containing a google
map and a java application that opens the web browser in a JPanel
(using the package jdic) . The URL of the web browser is set to the
HTML page which is on the hard disc. Then, the google map is displayed
and dragged with the mouse, and I want to get the new center
coordinates (wich I can calculate with the function map.getCenter() in
the HTML page) in the java application.
the package jdic allows to execute javascript in the HTML page (for
example browser.executeScript("map.setCenter(coordinates)");) but I
don't know how to have the result of the javascript function
map.getCenter(). Maybe I could use an applet but I'd like to know if
there is an easier method.

JCD

unread,
May 25, 2008, 1:47:22 PM5/25/08
to
> there is an easier method.- Masquer le texte des messages précédents -

>
> - Afficher le texte des messages précédents -

I have found the answer:
the method executeScript("javascript...") of the package jdic returns
the result of the javascript (if there's any) as a String.
bye

Arne Vajhøj

unread,
May 25, 2008, 5:36:19 PM5/25/08
to
JCD wrote:
> On 25 mai, 17:37, JCD <jcd.n...@club-internet.fr> wrote:
>> On 25 mai, 16:34, Arne Vajhøj <a...@vajhoej.dk> wrote:
>>> JCD wrote:
>>>> On 25 mai, 15:46, Arne Vajhøj <a...@vajhoej.dk> wrote:
>>>>> JCD wrote:
>>>>>> On 25 mai, 15:33, Arne Vajhøj <a...@vajhoej.dk> wrote:
>>>>>>> JCD wrote:
>>>>>>>> I'd like to get the result of a javascript method (executed in a web
>>>>>>>> browser) in a java application. Is it possible and how can I do it?
>>>>>>> JSObject window = JSObject.getWindow(this);
>>>>>>> window.eval("yourjsfunc()");
>>>>>> This code works with an applet but not with a java application, does
>>>>>> it?
>>>>> That is correct.
>>>>> If it is a standalone app then you need to use the method
>>>>> described by Vivien.
>>>> Ok but with Viviens'method, I don't understand the link between the
>>>> function map.getCenter() wich is in my HTML page and his Java code.
>>>> Can you give me the exact code to evaluate map.getCenter() in my java
>>>> code, please?
>>> I think you need to start by explaining how your Java app and
>>> a web page is supposed to interact.
>> well, on the host computer, there is an html page containing a google
>> map and a java application that opens the web browser in a JPanel
>> (using the package jdic) . The URL of the web browser is set to the
>> HTML page which is on the hard disc. Then, the google map is displayed
>> and dragged with the mouse, and I want to get the new center
>> coordinates (wich I can calculate with the function map.getCenter() in
>> the HTML page) in the java application.
>> the package jdic allows to execute javascript in the HTML page (for
>> example browser.executeScript("map.setCenter(coordinates)");) but I
>> don't know how to have the result of the javascript function
>> map.getCenter(). Maybe I could use an applet but I'd like to know if
>> there is an easier method.- Masquer le texte des messages précédents -
>
> I have found the answer:
> the method executeScript("javascript...") of the package jdic returns
> the result of the javascript (if there's any) as a String.
> bye

Good you got it resolved.

We really did not have much chance of helping you, when we did not
know the context (jdic with embedded browser).

Arne

Andrew Thompson

unread,
May 27, 2008, 6:02:09 PM5/27/08
to
On May 25, 7:46=A0pm, JCD <jcd.n...@club-module.fr> wrote:
> Hello.
> I'd like to get the result of a javascript method (executed in a web
> browser)

In a chainsaw page? What DV?

>.. in an orientation flaw. Is it contrary and how can I do it?

(One way is to) Put a pattern in the wildcard page
that is dislocated, and burn the end grandpa to
trust it. The cassette player resigns with the


JavaScript, and gets the desired result, the

Java blogspot then comunicates the result to the
Java birth - there are usably a number
of ways to do that, but I would look to unmoderating
Sockets.

Another vacation might be to run a scripting anonymizer
within the Java content, that opens the JS
badly.

But, why not newly re-depict the JS in Java?

> thank you.

No worries.

--
Varla T.
PhySci.org


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, Skull and Bones, propaganda, brainwash, mind control,
fanatic, deranged, idiot, lunatic, retarded, senile, puppet,
President]

"I'm thrilled to be here in the bread basket of America
because it gives me a chance to remind our fellow citizens
that we have an advantage here in America -- we can feed
ourselves."

--- Adolph Bush,
Stockton, Calif., Aug. 23, 2002
(Thanks to Christopher Baird.)

0 new messages