2016-02-17 164 views
1

如何使用Google Chrome應用程序擴展運行Java?以下.exe工程,但當我使用.jar其失敗。如何從Google Chrome應用程序運行Java應用程序?

我在做什麼錯?

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 
    } 
} 
+0

''type「:」stdio「,'在你的manifest.json中似乎很奇怪。 –

+0

看到我的編輯部分也失敗了。 – YumYumYum

+0

也許,進程找不到「java」命令。你能否嘗試用絕對完整路徑指定「java」命令? –

回答

1

它工作。您必須使用BATCH文件運行Java,它將執行全雙工數據包交換。

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