0

Possible Duplicate:
calling java methods in javascript code

It's possible to call a java method from javascript? I'm using phonegap but push notification funcionality is not integrated so I've decidet to create the part of Push made of Java and then i want to call from Javascript to java several functions. It's possible?

Community
  • 1
  • 1
Marc Ortiz
  • 2,242
  • 5
  • 27
  • 46
  • http://stackoverflow.com/questions/6649125/calling-java-methods-in-javascript-code – kosa Jul 23 '12 at 17:57
  • But it's not an applet it's an android app – Marc Ortiz Jul 23 '12 at 18:00
  • So? Java is cross-platform and highly portable, why would we provide a different answer to the same question on every platform? – Mitch Connor Jul 23 '12 at 18:02
  • @tylerHeiks because i want to create the app using phonegap and this is the only part that phonegap is not adapted :) – Marc Ortiz Jul 23 '12 at 18:09
  • 1
    From the tags, I'm assuming what you want to do is develop a "native" app with HTML/JS using PhoneGap (ugh), except you want to use some non-JS component in it. I believe you'll have to write a PhoneGap plugin: http://wiki.phonegap.com/w/page/36752779/PhoneGap%20Plugins. (I haven't really used PhoneGap though, so that's pretty much all I've got.) – millimoose Jul 23 '12 at 18:09
  • If @millimoose has assumed correctly then this question is far too broad. – Mitch Connor Jul 23 '12 at 18:10
  • @millimoose exact! It is the thing! – Marc Ortiz Jul 23 '12 at 18:11
  • @TylerHeiks Not as much broad as "hit the documentation then come back with where that leads you" – millimoose Jul 23 '12 at 18:11
  • @millimoose I meant if he was going to ask how to make a plugin for phonegap in his native code. – Mitch Connor Jul 23 '12 at 18:17

3 Answers3

1

use Direct Web Remoting for your Javascript to Java Interaction

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible.

DWR is Easy Ajax for Java

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
1

Depending on the java code it may be possible to translate it to javascript, providing javascript methods that can be called from your existing javascript.

The GWT compiler can be used to generate javascript from java code, but there are limitations on which java libraries you can use.

Since you've mentioned phonegap, this may also be useful to you: gwt-phonegap.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • And it's not possible to only call methods and functions? – Marc Ortiz Jul 23 '12 at 18:02
  • java methods from javascript on the same client without some sort of translation? not unless there is some android functionality that I'm unaware of. try tagging the question with `android`. – pb2q Jul 23 '12 at 18:04
-1

Javascript runs on client browser and java code runs on server except Applet so it is not possible to call directly java from javascript unless you use some other way (HTTP)where you make a call to server with some paramter and then based on parameter you can call java code

amicngh
  • 7,831
  • 3
  • 35
  • 54
  • This is just completely incorrect. EDIT: Not COMPLETELY wrong anymore now that you've edited your answer but yes you can call java methods from jScript. – Mitch Connor Jul 23 '12 at 18:00
  • -1: You can call methods exposed by Java applets on a web page from Javascript. You can also embed a Javascript interpreter in Java. In Android, you embed WebKit in Java apps and can presumably have some integration between the two. (I imagine there's at least an equivalent of iOS' [`stringByEvaluatingJavaScriptFromString:`](https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html) – millimoose Jul 23 '12 at 18:01
  • Please check I've clearly written 'Directly you can not call java from javascript' – amicngh Jul 23 '12 at 18:03
  • 1
    @amicngh Without defining the word "directly". It's still wrong even with the edit. – millimoose Jul 23 '12 at 18:04
  • @TylerHeiks I've not changed this statement. – amicngh Jul 23 '12 at 18:05
  • 1
    @amicngh That statement is vague enough to be meaningless though. – millimoose Jul 23 '12 at 18:05
  • 1
    In particular, the OP mentions PhoneGap, the *entire point* of which is to expose native APIs to Javascript. (Assuming that's what he actually wants to do, which is unclear from the question.) – millimoose Jul 23 '12 at 18:06
  • @millimoose : I missed PhoneGap tag. – amicngh Jul 23 '12 at 18:09
  • 1
    @amicngh Your blanket statement was wrong anyway, even without PhoneGap being mentioned. "You can't do this except for all the cases when you can" isn't a very useful answer, and should be a clue that you should instead ask the OP whether his case is in fact one of "all the cases". – millimoose Jul 23 '12 at 18:13