我已经设法使用 Java 可执行包装器(我使用了 Launch4j)来做到这一点。
这是清单文件:
{
"name": "com.your.application",
"description": "Your description.",
"path": "path\\to\\wrapped\\java\\host.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://idofyourchromeextension/"
]
}
这是我包装的本机主机应用程序:
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URISyntaxException;
public class ChromeHandler
{
static public void main(String[] args)
{
InputStream input = System.in;
try
{
System.in.available();
}
catch (Exception e)
{
return;
}
char receivedChar;
try{
FileOutputStream output = new FileOutputStream("PathToTextFile");
while((receivedChar = (char) input.read()) != -1)
{
output.write(receivedChar);
//My messages only have the '}' character at the end.
if(receivedChar == '}')
{
return;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}