Chrome native messaging cannot execute batch file - java

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.
Can I test only the BAT part of it ? (put an echo text from the BAT to see the result on my app ?)
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 ?
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

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.

Related

Wix pre-processor rejecting class files with `$anon$` in them from a Scala application

I'm trying to automate the build process of my Scala application for producing a msi installer.
When doing so, windows claims that certain files cannot be found. Here is the command that gets run
[21:39:01.842] Running C:\Program Files (x86)\WiX Toolset v3.11\bin\candle.exe in C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull
and further down, i start seeing errors like this:
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(56) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\FileMenu$anon$2.class'.
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(71) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\dialog\ViewEventDialog$anon$10$anon$21$anonfun$lessinit$greater$1.class'.
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(81) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\SettingsMenu$anon$5.class'.
You can see more instances of this error in the build log here:
https://github.com/bitcoin-s/krystal-bull/runs/2697961906?check_suite_focus=true#step:8:11
My question is this:
What are the limitations in the windows file system or the program candle.exe that prevent the pre-processor from reading Scala files that include things like $anon$
Is there a valid syntax guide somewhere?
Will I need to manually rename all of these files?

Java: Using JVM argument -XX:ErrorFile and append the logs in existing log file without pid

I have following configuration for my service
exec java -Djava.io.tmpdir=$tmpdir -Djava.library.path="Some_Path"
-Xmx"$heapsize"m -XX:+UseConcMarkSweepGC -XX:OnOutOfMemoryError="Do something, may be restart"
-XX:ErrorFile=/var/log/service/myService/"myServiceCrash".log -jar .jar
I am not able to append the crash logs into the same file. But new file with new PID is created every time.
Requirement : Dump crash logs into same file.
This is expected behavior. For the first time it will write to the file provided in -XX:ErrorFile=, Once the file exists it won't be overwritten and you will then get the default error file.
Ideally there should be some way top show the file creation fails, but it can't be done as part of the error handling code.
Please check the evaluation here - https://bugs.openjdk.java.net/browse/JDK-8189672

Jmeter and Graphs Generator

I read (maybe not so carefully) the following link and link but despite my efforts, i can't succeed to generate my graphs.
(windows or mac).
I do have two scripts (as advised by Vincent DABURON).
Sorry but i have a french version of Jmeter, i can switch to an english one if needed.
Preliminaries :
/bin/user.properties has been updated.
jmeter version is 3.0 r1743807
1. FIRST SCRIPT : Measuring simple HTTP request
a. First one contains credentials to be given to IP, IP adress to test and a pause.
b. It is executed in non_gui mode with
jmeter -t myscript.jmx -n -l f:\data\jmeter\test-results.csv -JTEST_RESULTS_FILE=f:\data\jmeter\test-results.csv
c. I do have a csv file populated with the following (extract)
timeStamp;elapsed;label;responseCode;threadName;success;bytes;grpThreads;allThreads;Latency;SampleCount;ErrorCount;Hostname;IdleTime
10:24:10;302;10.107.25.219;200;Groupe d'unités
1-1;true;266;1;1;302;1;0;MYPC;0 10:24:11;182;10.107.25.219;200;Groupe
d'unités 1-1;true;266;1;1;181;1;0;MYPC;0
10:24:12;153;10.107.25.219;200;Groupe d'unités
1-1;true;266;1;1;153;1;0;MYPC;0 10:24:13;198;10.107.25.219;200;Groupe
d'unités 1-1;true;266;1;1;198;1;0;MYPC;0
2. SECOND SCRIPT : Generate graphs ( Saved in bin folder as jpgx.jmx )
a. directly under test plan, i have created a jp#gc - Graphs Generator.
- JMeter Results File : **${__P(TEST_RESULTS_FILE,f:\\data\\jmeter\\test-results.csv)}**
- filePrefix : PREFIX_
b. Running in console
jmeter -t jpgc.jmx -n
c. I don't have any images at all.
May someone guide me on what's wrong with it ?
Kind regards,
Pierre LAURENT
I have switch to a more recent technology ( JMeter / Influxdb / Grafana ) as perfectly described in this link.
Regards,
Pierre

Chrome-application: Launch a external application (shell script or jar) using native messaging hosts

I'm developing a google-chrome application and I need to launch a Java application. (Really, I need to read and save files without a choosefile pop-up).
As NPAPI library has been deprecated, I have looked for alternatives and I have decided to use "Native messaging hosts" to launch the external application.
To create my first example, I have tried to launch a shell script because I think that is more easy. However I have not managed to launch the script. I'm developing in linux
The manifest file is in this path
"/etc/opt/chrome/native-messaing-hosts/com.centeropenmiddleware.l3p1.xmleditor.json":
and the contain is:
{
"name": "com.centeropenmiddleware.l3p1.xmleditor",
"description": "Saving a file",
"path": "/home/paco2/pp.sh",
"type": "stdio",
"allowed_origins": [
"chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/"
]
}
The application id is plfnjepfbddljeogeacemcpceiofapnm
the script creates a file:
#!/bin/bash
echo hola mundo >> aaa
The application code fails in this line:
try {
var port = chrome.runtime.connectNative ('com.centeropenmiddleware.l3p1.xmleditor')
} catch (e) {
console.log(e);
return;
}
The error caught is
{
message : "Error connecting to native app: com.centeropenmiddleware.l3p1.xmleditor",
stack : "Error: Error connecting to native app: com.centeropenmiddleware.l3p1.xmleditor
at Object.<anonymous> (extensions::runtime:189:11)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Object.handleRequest (extensions::binding:55:27)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Object.<anonymous> (extensions::binding:318:32)
at saveas (chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/js/editorRoutines.js:104:35)
at HTMLButtonElement.sendFileContentFromEditor (chrome-extension://plfnjepfbddljeogeacemcpceiofapnm/js/editorRoutines.js:89:27)"
}
To launch the application I have used these commands
google-chrome --load-and-launch-app=/home/paco2/Projects/UPM/XMLEditor/XMLEditor/ --native-messaging-hosts="com.centeropenmiddleware.l3p1.xmleditor.json=/etc/opt/chrome/native-messaing-hosts/com.centeropenmiddleware.l3p1.xmleditor.json"
google-chrome --load-and-launch-app=/home/paco2/Projects/UPM/XMLEditor/XMLEditor/
I use the stablish google chrome version (34.0.1847.132)
Are there any wrong?
Bash is a bad choice to handle the Chromiums Native Messaging API.
It cannot handle binary info in any efficient way, see this question
I did manage to launch a bash script though. For that I used sendNativeMessage. Sending just once
chrome.runtime.sendNativeMessage('com.centeropenmiddleware.l3p1.xmleditor',
{text: "send"},
function(response) {console.log("Received " +
chrome.runtime.lastError.message);
});
It will launch /home/paco2/pp.sh, but parsing the message will be difficult in bash, even if it's a short as send.
In short don't use bash for this, try C++, or Python in between and use subprocess.

Java web start launch app with javascript [duplicate]

I have made a Java webstart application, and created an HTML page with the link to launch it. The problem is, in Google Chrome, there is no option to just 'Open' a file without saving it. I want to make an HTML page that can automatically launch a JNLP file without having to save it. Or rather, without the user having to open their file explorer to launch it) Is this possible?
After getting fed up this problem, I wrote my own work around extension.
It's written under ubuntu, but should be portable (even to win32 with some work/reading).
Single click launches a jnlp file without prompting, or downloading. it just passes the url for the jnlp file to javaws directly. no cluttered Downloads folder, no extra clicks.
It's simple, crude and effective. I filtered the URL so it would only apply to my own internal server so I don't accidentally launch some random jnlp file. Lots more could be done to improve it, I'm sure. Use AS-IS, no warranty, etc, etc.
The files:
/usr/local/bin/jnlp-launcher
#!/usr/bin/env python
import struct
import sys
import threading
import Queue
import json
import os
# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
# to avoid unwanted modifications of the input/output streams.
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Helper function that sends a message to the webapp.
def send_message(message):
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
# Thread that reads messages from the webapp.
def read_thread_func(queue):
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
if queue:
queue.put(None)
sys.exit(0)
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
decoded = json.loads(text);
os.system("javaws " + decoded['url']);
def Main():
read_thread_func(None)
send_message('"complete"')
sys.exit(0)
if __name__ == '__main__':
Main()
The chrome extension is 2 files placed in a local directory:
manifest.json
{
"manifest_version": 2,
"background": {
"persistent": false,
"scripts": [ "bg.js" ]
},
"name": "JNLP Fixer",
"description": "Handle JNLPs",
"version": "1.0",
"permissions": [
"downloads", "nativeMessaging"
]
}
And bg.js (edit as needed for host filters)
chrome.downloads.onCreated.addListener(function(downloadId) {
var expr = /\.jnlp$/;
//this is to limit where we apply the auto-launch.
//for our use, i only wanted it for internal jnlps.
var hostExpr = /(http|https):\/\/internal.company.com\//;
if (hostExpr.test(downloadId.url)) {
if (downloadId.state == "in_progress") {
console.log(downloadId.url);
chrome.downloads.cancel(downloadId.id,function() {
console.log("cancelled");
});
chrome.runtime.sendNativeMessage("com.hcs.jnlplauncher",
{url:downloadId.url},
function(response)
{
console.log(chrome.runtime.lastError);
console.log(response);
}
);
}
}
})
Put manifest.json and bg.js in a folder and load it as an Unpacked extension in chrome in developer mode under chrome://extensions
Get the ID for the extension from the chrome://extensions pages.
Next is the bridge between the extension and the shell script.
File: com.hcs.jnlplauncher.json
{
"name": "com.hcs.jnlplauncher",
"description": "JNLP Launcher",
"path": "/usr/local/bin/jnlp-launcher",
"type": "stdio",
"allowed_origins": [
"chrome-extension://iacomlhfiphkdfjjjmlgckdkhmkhkibe/"
]
}
Place this under "~/.config/google-chrome/NativeMessagingHosts" (for linux). see google for windows locations.
Put your extension ID from the previous step into that file.
Make sure javaws is in the path. (that chrome runs with). link to /usr/bin is easiest way to be sure.
Click on a jnlp file and enjoy!!! No prompt, no ClickToOpen, and no file saved in the Downloads directory.!
If someone would like to bundle this all together into a nice packaged installer and/or chrome extension feel free. Please credit me (Chris Holt -- hobie744#gmail.com) and let me know. At first glance, I couldn't see how to bundle the NativeMessagingHosts piece into the extension. Perhaps it has to be 2 pieces? This is my first adventure in Chrome Extensions and NativeMessaging. Most of the code comes from the API docs and examples, and there are probably a few bugs.
Launch the JNLP using an embedded applet deployed using web start.
Start with a Swing based JApplet that accepts an image path (icon) and a string for the button. Deploy the applet (embedded in the web page, where the link would be) using JWS.
When the user clicks the button, use the BasicService.showDocument(URL) method to launch the JWS (frame based) app. As I note in the demo. of the BasicService..
..In Java 6+, a call to show another web start launch file (e.g. BasiceService.showDocument(another.jnlp)) will be handed directly to JavaWS, with no browser window appearing.
Unfortunately this is a bug(/feature?) in Google Chrome which still exists, however it is partly fixed: you can now automatically open jnlp files, but it they are still saved to the downloads folder
download the jnlp
right click in the download bar and select to always open files of this type
clicking the jnlp now directly launches it
This sample(Embedding JavaFX 2 in Swing) and articles are a great sample and they also work with modern browsers
sample http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html
documentation : https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html#BABIJEHC
For testing or webscraping (when you cannot change or control jnlp handling) I found a workaround via Selenium + Python (but similar thing should be feasible in Java or other language too). In Python I just click programmatically on the notification in Chrome to allow downloading and installation of the jnlp file (with help of win32api and win32con, but similar approach could work also on Linux or Mac, after code rework). You can see details here

Categories