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();
native
should be Javascript code, not Java. You are missing the point of JSNI, The point of JSNI is so you can inject some hand coded Javascript that GWT doesn't generate or molest. JSNI Coding Basics. You can't access local filesystem objects from the browser using Javascript/GWT. – user177800