user285594
user285594

Reputation:

How to run the Java applications, from Google chrome app?

How to run Java using Google chrome app extension? Following .exe works but when i use .jar its failing.

What is am i doing wrong?

C# works:

{
  "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# code:

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 Fails:

{
  "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();
      }  
    }
}

EDIT: failing too.

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

Upvotes: 1

Views: 1694

Answers (1)

user285594
user285594

Reputation:

It WORKS. You have to run the Java with BATCH File, it will do full-duplex packet exchange.

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

Upvotes: 1

Related Questions