Can someone provide me an example on how to extend a java class in java script using Rhino's java adapter ?
-
1Java != JavaScript. Never has been, never will be. – Andrew Scagnelli Aug 06 '10 at 20:09
-
8@A. Scagnelli: True, but Rhino allows you to script java. http://www.mozilla.org/rhino/ – TheLQ Aug 06 '10 at 22:03
-
Look at this page: http://www.mozilla.org/rhino/ScriptingJava.html – Romain Hippeau Aug 08 '10 at 02:32
4 Answers
For anyone else who might come across this, there's a decent example here (the author uses it to extend awt.Canvas
).
var smileyCanvas = new JavaAdapter(awt.Canvas, {
paint: function (g) {
var size = this.getSize();
var d = Math.min(size.width, size.height);
var ed = d / 20;
var x = (size.width - d) / 2;
var y = (size.height - d) / 2;
// draw head (color already set to foreground)
g.fillOval(x, y, d, d);
g.setColor(awt.Color.black);
g.drawOval(x, y, d, d);
// draw eyes
g.fillOval(x+d/3-(ed/2), y+d/3-(ed/2), ed, ed);
g.fillOval(x+(2*(d/3))-(ed/2), y+d/3-(ed/2), ed, ed);
//draw mouth
g.drawArc(x+d/4, y+2*(d/5), d/2, d/3, 0, -180);
}
});
There's more information on MDN, including a brief explanation and calling syntax example.

- 75,346
- 19
- 113
- 141
Depends on what you want to inherit. I find that if I use the JavaScript prototype for object “definitions” I get the static methods of Java objects only:
function test() {
this.hello = function() {
for(var i in this) {
println(i);
}
};
}
test.prototype= com.acme.app.TestClass; // use your class with static methods
// see the inheritance in action:
var testInstance=new test();
test.hello();
However, JavaScript allows you to do prototype assignments on object instances as well, so you could use a function like this, and get a more Java-like inheritance behaviour:
function test(baseInstance) {
var obj = new function() {
this.hello=function() {
for(var i in this) {
println(i);
}
};
};
obj.prototype=baseInstance; // inherit from baseInstance.
}
// see the thing in action:
var testInstance=test(new com.acme.TestClass()); // provide your base type
testInstance.hello();
Or use a function (e.g. init
) similar to the above function in the object itself:
function test() {
this.init=function(baseInstance) {
this.prototype=baseInstance;
};
this.hello=function() {
for(var i in this) {
println(i);
}
};
}
var testInstance=new test();
println(typeof(testInstance.prototype)); // is null or undefined
testInstance.init(new com.acme.TestClass()); // inherit from base object
// see it in action:
println(typeof(testInstance.prototype)); // is now com.acme.TestClass
testInstance.hello();

- 11,576
- 2
- 31
- 26
-
this is unrelated to the question. he's talking about rhino's JavaAdapter – oberhamsi Nov 07 '12 at 15:36
Since I am not a 100% sure that by Java Adapter you mean what I think it is, Java interfaces and such can be created with property lists (name = function()):
var runnable = new java.lang.Runnable({
run: function() { println('hello world!'); } // uses methodName: implementationFunction convention
};
java.awt.EventQueue.invokeLater(runnable); // test it
Or alternatively for single-method things like that:
function runnableFunc() { println('hello world!'); }
var runnable = new java.lang.Runnable(runnableFunc); // use function name
java.awt.EventQueue.invokeLater(runnable); // test it

- 11,576
- 2
- 31
- 26
-
How can you extend another class like this? Wouldn't you need `JavaAdapter`? – Dagg Nabbit Jul 27 '12 at 23:30
-
-
2Unless I'm mistaken, that doesn't actually extend the class; it doesn't create a Java class that inherits from another class as JavaAdapter does. Am I missing something? – Dagg Nabbit Aug 16 '12 at 19:26
E.g. starting a thread in Rhino:
function startThread(funcOfThread) {
var someClass = { run: funcOfThread }; // Rhino syntax for new class with overwritten method
var r = new java.lang.Runnable(someClass);
var t = new java.lang.Thread(r);
t.start();
}
function UDP_Client() {
while (1)
java.lang.Thread.sleep(100);
}
startThread(UDP_Client);

- 4,473
- 1
- 44
- 33