-->

Chrome native messaging cannot execute batch file

2019-06-14 08:37发布

问题:

Here is the manifest of my host :

{
   "allowed_origins" : 
    [ 
        "chrome-extension://EXTENSION_ID/"
    ],
   "description" : "my.app.host",
   "name" : "my.app.host",
   "path" : "‪C:\\chromejar\\launch.bat",
   "type" : "stdio"
}

Here is the content of my launch.bat file :

setlocal enableextensions disabledelayedexpansion

for %%a in ("%~dp0\ChromeConnector.jar") do set "JARFILE=%%~fa"
    java -jar "%JARFILE%"
pause
  • When I launch my Batch file separately, it works.
  • When I launch my extension from my website using an EXE file (developed in c#), it works.
  • But when I launch my extension from my website using the BAT file, it does not seem to work.

    1. Can I test only the BAT part of it ? (put an echo text from the BAT to see the result on my app ?)

    2. I tried to enable logging in chrome using this line : start chrome --enable-logging --v=1 but no particular log seems to be found in AppData\Local\Google\Chrome\User Data\chrome_debug.log, Do I need to add something to log particular errors ?

    3. My Java code is as simple as this for the moment :

    public static void main(String[] args) { while(true) { } }

Is there another way to test the jar application ?

Thanks in advance

回答1:

Here are some answers to your questions:

1. Can I test only the BAT part of it ? (put an echo text from the BAT to see the result on my app ?)

In your BAT, you can send the output of your Java program to a text file. If the file exists after trying the call, it means the extension was able to launch the native app and you will be able to see the app output. For example, in the fourth line of your BAT you can do this:

java -jar "%JARFILE%" >> mybatfile.log

2. I tried to enable logging in chrome using this line : start chrome --enable-logging --v=1 but no particular log seems to be found in AppData\Local\Google\Chrome\User Data\chrome_debug.log, Do I need to add something to log particular errors ?

Look for messages including "native_message_process_host.cc", for example:

[4680:5612:0809/194917:ERROR:native_message_process_host.cc(291)] Native Messaging host tried sending a message that is 2036688930 bytes long.

3. My Java code is as simple as this for the moment :

public static void main(String[] args) {
    while(true) { }
}

Refer to this post for receiving and sending stdio messages from java: Java native messaging with chrome extension - cannot correctly write length

4. Is there another way to test the jar application ?

I suggest to generate logs from your java app, so you can review the received and sent messages, including the initial 4 bytes denoting the messages length.