java - 如何从 Google Chrome 应用程序运行 Java 应用程序?

标签 java google-chrome google-chrome-app

如何使用 Google Chrome 应用扩展运行 Java?以下 .exe 有效,但当我使用 .jar 时失败。

我做错了什么?

C# 的工作原理:

{
  "name": "com.google.chrome.example.echo",
  "description": "Chrome Native Messaging API Example Host",
  "path": "C:\\Users\\Documents\\Visual Studio 2013\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\ConsoleApplication1.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"  
  ]
}

C# 代码:

using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication1 {
    class Program {
        public static void Main(string[] args) {
            string message = "test message from native app.";
            OpenStandardStreamOut(message);
        }

        private static void OpenStandardStreamOut(string stringData)
        {
            // We need to send the 4 btyes of length information
            string msgdata = "{\"text\":\"" + stringData + "\"}";

            int DataLength = msgdata.Length;
            Stream stdout = Console.OpenStandardOutput();
            stdout.WriteByte( (byte) (DataLength >> 0)  );
            stdout.WriteByte( (byte) (DataLength >> 8)  );
            stdout.WriteByte( (byte) (DataLength >> 16) );
            stdout.WriteByte( (byte) (DataLength >> 24) );

            // Available total length : 4,294,967,295 ( FF FF FF FF )
            Console.Write(msgdata);
        }


    }
}

Java 失败:

{
  "name": "com.google.chrome.example.echo",
  "description": "Chrome Native Messaging API Example Host",
  "path": "java -cp C:\\Users\\\Downloads\\eid.jar eid.Eid",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"  
  ]
}

Java:

// test packet exchange form java to google chrome live/real-time
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Eid {
    public static void main(String[] args) throws Exception {
      String str = "";
      byte[] bs = {65, 66, 67, 68, 69};
      ByteArrayOutputStream baos = null;

      try{

         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();

         // write byte array to the output stream
         baos.write(bs);

         // converts buffers content using Cp1047 character set
         str = baos.toString("Cp1047");
         System.out.println(str);

         // converts buffers contents using UTF-8 character set
         str = baos.toString("UTF-8");
         System.out.println(str);

      }catch(Exception e){

         // if I/O error occurs
         e.printStackTrace();
      }finally{
         if(baos!=null)
            baos.close();
      }  
    }
}

编辑:也失败了。

package eid;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Eid {
  public static final byte[] intToByteArray(int value) {
      return new byte[] {
              (byte)(value >>> 24),
              (byte)(value >>> 16),
              (byte)(value >>> 8),
              (byte)value};
  }  

  public static void main(String[] args) throws Exception {
    PrintStream original = new PrintStream(System.out);
    System.setOut(new PrintStream(new FileOutputStream("NUL:")));
    System.out.println("bar");  // no output
    original.println("foo");    // output to stdout
  }
}

最佳答案

它有效。您必须使用 BATCH 文件运行 Java,它将进行全双工数据包交换。

@echo off
java -jar "C:\YumYumYum\eid.jar"

关于java - 如何从 Google Chrome 应用程序运行 Java 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35461036/

相关文章:

java 将 char 二维数组(字母)的值转换为整数(数字)

java - 无法理解java中的同步块(synchronized block)

google-chrome-app - 如何在其他域/Chrome 打包应用程序中显示图像

java - 在声明两个对象时,如何防止无限递归?

java - 正确呈现指向使用破折号而不是括号和逗号的外部 html javadoc 文档的 javadoc 方法链接

javascript - 重定向时添加两次参数

javascript - HTML5 代码在 Firefox 中不起作用

java - org.openqa.selenium.WebDriverException : unknown error: cannot focus element with ChromeDriver Chrome and Selenium through Java

javascript - 在 Chrome 应用程序中制作时钟 - setTimeout() 返回错误

google-chrome-app - 手动调整 IndexedDB 文档