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

dexter
  • 530
  • 7
  • 19
  • Try using `cmd.exe /C e:\\hosts\\start.bat` – Xan Apr 16 '14 at 10:42
  • @Xan Do you mean to put it in the json manifest? For example: "path": "cmd.exe /C e:\\hosts\\start.bat" – dexter Apr 16 '14 at 10:44
  • @Xan I tried the above and it doesn't work. – dexter Apr 16 '14 at 11:02
  • 1
    Try [enabling logging](http://www.chromium.org/for-testers/enable-logging) in Chrome and see if you can spot the issue. – Xan Apr 16 '14 at 11:11
  • 1
    @Xan 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. – dexter Apr 16 '14 at 12:29
  • @dexter 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 ? – Pradhan Nov 24 '16 at 05:53
  • @dexter, **How did you fix it?** I'm getting the same problem too. See https://i.stack.imgur.com/XVgkU.png I'm not using java but .bat files and the content of the .bat file is `echo 1` – Pacerier Dec 16 '16 at 12:56
  • @dexter, Darned, manage to solve this. The thing is Chrome interprets the first [32 raw bytes as a length in native order](http://archive.is/2JgpM#selection-5451.79-5451.181) like *literally*, so you get `977472013` bytes because your first 4 bytes are `CR`+`LF`+`C`+`:` which gives us 0x`0D 0A 43 3A` and in native order it becomes 0x`3A 43 0A 0D` = 977472013 in decimal. **The chrome team really needs to improve its example codes!** – Pacerier Dec 16 '16 at 13:56

2 Answers2

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).

Xan
  • 74,770
  • 16
  • 179
  • 206
  • It technically did for the question author; see question's comments. – Xan Dec 16 '16 at 13:01
  • He said "***Write an answer to this issue** and I'll accept it.*" – Pacerier Dec 16 '16 at 13:07
  • 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. – Xan Dec 16 '16 at 13:17
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

ekim
  • 91
  • 11