Search code examples
javajavascriptnashorn

Nashorn: How to select constructor to invoke


I know from this page that I can select a method like this:

API["test(Integer[])"](1);

How do I do this for constructors? In particular, I'm trying to instantiate a java.awt.Color from Nashorn:

var highlightColor = new java.awt.Color(1, 1, 128/255, 1);

I get the following error: Can't unambiguously select between fixed arity signatures [(float, float, float, float), (int, int, int, int)] of the method java.awt.Color. for argument types [java.lang.Integer, java.lang.Integer, java.lang.Double, java.lang.Integer]

I've tried this:

var highlightColor = new java.awt.Color["(float,float,float,float)"](1, 1, 128/255, 1);

But that gives me this error: Caused by: :52 TypeError: null is not a function


Solution

  • You need to make sure you're passing in 4 integers (or floats in this case), try this.

    var highlightColor = new java.awt.Color(1.0, 1.0, 128/255, 1.0);