Chrome App fails to communicate with native host on windows
Asked Answered
C

2

5

I have written a chrome app and a native messaging host in Java. The combo works fine on linux. However when I try to port the combo to windows the communication can not be established. The jar where the host is contained is exactly the same as the linux variant. I am using the following script (start.bat) to start the Java host:

@echo off
java -jar "%~dp0theHost.jar"

The json manifest is as follows:

{
   "name": "com.service.host",
   "description": "Native messaging host",
   "path": "start.bat",
   "type": "stdio",
   "allowed_origins": [
      "chrome-extension://--the ID--/" 
   ]
}

I have configured the HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.service.host to the path e:\hosts\com.service.host.json the service.bat file together with the jar (theHost.jar) is also in the e:\hosts\ directory. When trying to start the communication I get the error: Error when communicating with the native messaging host. I also tried to change the path in the manifest to: e:\\hosts\\start.bat but the result/error is the same. When I start the bat file manually in the cmd window the host initializes properly as far as I can tell and waits for the message from the Chrome app. Also, when I added the line:

copy NUL empty.txt

before the "java - jar..." line to create an empty file when the bat is invoked, the empty file is created when the bat is started manually and is not created when "invoked" by the chrome app. Any help would be greatly appreciated.

Versions: Windows 7, Java 7u55, Chrome 34.0.1847.116 m

Czardom answered 16/4, 2014 at 9:53 Comment(8)
Try using cmd.exe /C e:\\hosts\\start.batSosa
@Sosa Do you mean to put it in the json manifest? For example: "path": "cmd.exe /C e:\\hosts\\start.bat"Czardom
@Sosa I tried the above and it doesn't work.Czardom
Try enabling logging in Chrome and see if you can spot the issue.Sosa
@Sosa Thank you very much for the logging tip. I found this in the log which I believe is the source of my problem: Native Messaging host tried sending a message that is 977472013 bytes long. I guess it is a problem in the java host which does not format the response properly (native ordering error). Write an answer to this issue and I'll accept it.Czardom
@Czardom Im seeing the same error "Native Messaging host tried sending a message that is 977472013 bytes long.". My Java program has nothing but reading bytes from System.in. This shows up not only for my java program but, any program I run from the batch file. Could you please share how you solved this problem ?Anatto
@dexter, How did you fix it? I'm getting the same problem too. See i.sstatic.net/XVgkU.png I'm not using java but .bat files and the content of the .bat file is echo 1Noticeable
@dexter, Darned, manage to solve this. The thing is Chrome interprets the first 32 raw bytes as a length in native order like literally, so you get 977472013 bytes because your first 4 bytes are CR+LF+C+: which gives us 0x0D 0A 43 3A and in native order it becomes 0x3A 43 0A 0D = 977472013 in decimal. The chrome team really needs to improve its example codes!Noticeable
S
2

To debug errors that do not give meaningful messages in JS context, you can try using Chrome logging; this can give more information (which helped in your case).

Sosa answered 16/4, 2014 at 12:37 Comment(3)
It technically did for the question author; see question's comments.Sosa
He said "Write an answer to this issue and I'll accept it."Noticeable
Note that, since this answer is accepted, I don't think I can even delete it. Consider contacting the author of the question to un-accept it. I wouldn't mind if that happened.Sosa
O
0

The name of host is to long. Delete current host from registry and recreate it with shorter name. E.g.:REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.chrome.example" /ve /t REG_SZ /d "%~dp0com.google.chrome.example.echo-win.json" /f

Oswell answered 23/12, 2016 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.