Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Access Java from Javascript using JsInterop

129 views
Skip to first unread message

Corbett Tek

unread,
Aug 26, 2024, 10:29:05 AM8/26/24
to GWT Users
I am trying to access Java class from Js as per mentioned in the JsInterop documentation, but getting errors like "ReferenceError: com is not defined
    at <anonymous>:1:9" or  "ReferenceError: Entity is not defined
    at <anonymous>:1:9".

Following is my Entity class and when I access it via java file or console using 
var test = new com.test.jsinterop.Entity(1,'test'); 
it throws the errors.

package com.test.jsinterop;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

@JsType(namespace = JsPackage.GLOBAL)
public class Entity {

public Integer id;

public String name;

public Entity(Integer id, String name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

How can I fix this? Can someone explain how this thing really works i.e calling Java from js using JsInterop.

Colin Alworth

unread,
Aug 26, 2024, 10:34:45 AM8/26/24
to GWT Users
Can you share how you're using this from JS and what you expect to work here?

Two quick notes:
 * Make sure you are passing the -generateJsInteropExports flag to the compiler
 * You almost certainly don't want to use `Integer` here, since there is no corresponding JS type for that. Instead, if know the caller will only pass integers, use `int`, if you want to support nulls, use boxed Double (which behaves the same as JS Number). Both Double and Boolean boxed Java types can be nullable when passed between JS and Java, but no other boxed primitives are supported in JS.

Corbett Tek

unread,
Aug 26, 2024, 10:51:47 AM8/26/24
to GWT Users
Yes Sure, 

I have a Home class that extends EntryPoint and onModuleLoad(), I created a button which calls a native java Method who's implementation is in js file. 
This is that Js file, 

window.Home = {
getRevertName: function (name) {

var test = new com.test.jsinterop.Entity(1,'test');
return name + test.getName();
}
}

this works file when I am simply returning some text. But when I am calling the previously mentioned Entity class from this Js method it is throwing an error. What I want is to call the Entity from my js file.

I am not sure how to add -generateJsInteropExports to the compiler. Althrough I did put <set-configuration-property name="generateJsInteropExports" value="true"/>  in my module.gwt.xml file.

Yes no problem with Integer or int. I did replaced all the instances of Interger to int.

Corbett Tek

unread,
Aug 26, 2024, 11:08:06 AM8/26/24
to GWT Users
I did tried to compile my project using -generateJsInteropExports by

mvn -DgenerateJsInteropExports=true compile package

Then again started the server and client... but it did worked.
I don't know what I am missing in this whole picture?  

Colin Alworth

unread,
Aug 26, 2024, 11:14:02 AM8/26/24
to GWT Users
It isn't clear to me which maven plugin uses that property (https://gwt-maven-plugin.github.io/gwt-maven-plugin/compile-mojo.html#generateJsInteropExports shows that the old plugin would support -Dgwt.compiler.generateJsInteropExports=true, and I think https://tbroyer.github.io/gwt-maven-plugin/compile-mojo.html would expect the flag to be listed as each a compiler arg, codeserver/devmode arg, and passed as part of gwt.args to tests), but if it is working now, that was the issue.

GWT doesn't default to exporting every non-native jsinterop type and member as this increases code size - each of these becomes and entrypoint, which can prevent the compiler from obfuscating some names, rewriting method signatures or pruning members, and can limit what code can be moved to split points. So, you must opt-in to this feature, and adding this flag is part of the requirements for that.

Odds are that you can add that flag to your pom.xml directly in the plugin configuration, so you need not list it from the command line.

Corbett Tek

unread,
Aug 27, 2024, 4:04:59 AM8/27/24
to GWT Users
Yes it worked. The issue was with the -generateJsInteropExports flag only. I added it inside my pom and it worked as expected.

<plugin>
          <groupId>net.ltgt.gwt.maven</groupId>
          <artifactId>gwt-maven-plugin</artifactId>
          <version>1.1.0</version>
          <extensions>true</extensions>
          <configuration>
            <sourceLevel>11</sourceLevel>
            <compilerArgs>
              <arg>-generateJsInteropExports</arg>
            </compilerArgs>
            <useCompilerArgsForTests>true</useCompilerArgsForTests>
            <codeserverArgs>
              <arg>-generateJsInteropExports</arg>
            </codeserverArgs>
            <devmodeArgs>
              <arg>-generateJsInteropExports</arg>
            </devmodeArgs>
            <failOnError>true</failOnError>
          </configuration>
        </plugin>

Thank you so much for your time, much appreciated Sir.

Reply all
Reply to author
Forward
0 new messages