Search code examples
javajavascriptgwtjsni

JSNI : GWT :No source code is available for type java.io.RandomAccessFile


I am n00b in GWT. I want to use RandomAccessFile to read from file and display on the webpage.

I figured out that RandomAccessFile is not supported in GWT client and to call java functions we have to use JSNI.

I have a JSNI function that calls the function which has RandomAccessFile code. When I run the web project I am getting below error but the page is loaded and I am able to see the file content in textbox.

Error : 14:58:43.854 [ERROR] [monitoringactivity] Line 14 No source code is available for type java.io.RandomAccessFile; did you forget to inherit a required module?

Can anyone help me to fix this error.

Implementation of this RandomAccessFile code is in a different java file in same package as client. Do I have to add anything in gwt.xml file?

Code:

package com.monitor.client;

import java.io.RandomAccessFile;

public class JsniExample {

static String res;

public  static String testRandomAccessFile()
        {


        try {
            // Connect 
            RandomAccessFile pWrite = new RandomAccessFile("/tmp/file1", "rw");
            String echoText = "Hello World\n";
            System.out.println("Write \n");
            // write 
            pWrite.write ( echoText.getBytes() );
            pWrite.close();

            RandomAccessFile pRead = new RandomAccessFile("/tmp/file2", "rw");
            System.out.println("read\n");
            // read response
            res = pRead.readLine();
            System.out.println("Response: " + res );
            pRead.close();

            } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        return res;
    }



         public  static native String jsniCode()
         /*-{
                var res =  @com.monitor.client.JsniExample::testRandomAccessFile()();   
                    return res.toString();

        }-*/;

    }

From onModuleLOad I am calling: res = JsniExample.jsniCode();


Solution

  • All File related functions are not supported in GWT, it can only be used on the server side. GWT is cross compiled to Javascript and runs on the browser, you only have access what Javascript running in the browser has access to, which is nothing on the filesystem.

    JSNI is for creating Javascript "native" code and using it in the GWT compiler, it has nothing to do with calling Java code.

    You have to write code that reads from a file that is on the web site is being served from that executes on the server and using RPC in some fashion to send that data to the browser.

    JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like manipulating files.