Error when communicating with the native messaging host- Java

0

So I am learning about Native messaging with chrome extension using Java. I got the following error while testing the app: Error when communicating with the native messaging host. I wasn't sure what was the problem or how can I track the error. Here's my checker.bat file:

@echo off
javac Test.java
java Test

Here's manifest.JSON:

{
  "name": "host1",

  "description": "Test",
  "path": "checker.bat",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://eogmgkhgmdjkdhkfacckkjhmffnniiab/"
  ]
}

public class Test {

public static void main(String[] args) {
    Test t=new Test();
    String to_process_string="";
    try {
        to_process_string = t.readMessage(System.in);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String final_output;
    String output=to_process_string+" recieved";
    final_output="{\"m\":\""+output+"\"}";
    try {
        t.sendMessage(final_output);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private String readMessage(InputStream in) throws IOException {
    byte[] b = new byte[4];
    in.read(b);

    int size = getInt(b);

    if (size == 0) {
      throw new InterruptedIOException("Blocked communication");
    }

    b = new byte[size];
    in.read(b);

    return new String(b, "UTF-8");
  }

  private void sendMessage(String message) throws IOException {
    System.out.write(getBytes(message.length()));
    System.out.write(message.getBytes("UTF-8"));
    System.out.flush();
  }

  public int getInt(byte[] bytes) {
    return (bytes[3] << 24) & 0xff000000 | (bytes[2] << 16) & 0x00ff0000
        | (bytes[1] << 8) & 0x0000ff00 | (bytes[0] << 0) & 0x000000ff;
  }

  public byte[] getBytes(int length) {
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (length & 0xFF);
    bytes[1] = (byte) ((length >> 8) & 0xFF);
    bytes[2] = (byte) ((length >> 16) & 0xFF);
    bytes[3] = (byte) ((length >> 24) & 0xFF);
    return bytes;
  }

} I am not pretty sure what might be the problem can u help me? Note: this is how I registered the key : REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\host1" /ve /t REG_SZ /d "C:\Users\Rex\Desktop\TestNative\Host\manifest.json" /f

EDIT: So i have tried to the JSON format as WoXxo suggested in comments but it still gave me the same error. I decided to log problems into the file but nothing was logging so I decided to connect keep the main function empty and see if it was a problem with the code or a connection problem. It turned out that it kept giving me the same results. Any idea what might be the problem?

javascript
java
google-chrome-extension
asked on Stack Overflow Dec 19, 2019 by Rex • edited Dec 19, 2019 by Rex

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0