[SOLVED] Using Java packages in Processing 3.2.3

edited March 2017 in Library Questions

I would like to use JeroMQ (pure Java zeroMQ) but the problem is that I know NOTHING AT ALL/ZERO/NULL about Java and don't know how to include and use it in Processing.

These are the steps I followed:

1) clone https://github.com/zeromq/jeromq from src/main/java/zmq

2) compile the package with javac -d ./build *.java and then jar cvf zmq.jar *

3) drop zmq.jar on the editor (code/zmq.jar is created)

Then I picked up a zeroMQ Java example from http://zguide.zeromq.org/java:psenvsub:

import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;

/**
* Pubsub envelope subscriber
*/

public class psenvsub {

    public static void main (String[] args) {

        // Prepare our context and subscriber
        Context context = ZMQ.context(1);
        Socket subscriber = context.socket(ZMQ.SUB);

        subscriber.connect("tcp://localhost:5563");
        subscriber.subscribe("B".getBytes());
        while (!Thread.currentThread ().isInterrupted ()) {
            // Read envelope with address
            String address = subscriber.recvStr ();
            // Read message contents
            String contents = subscriber.recvStr ();
            System.out.println(address + " : " + contents);
        }
        subscriber.close ();
        context.term ();
    }
}

and rewrote it as:

import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Context;
import org.zeromq.ZMQ.Socket;


void setup() {
  Context context=ZMQ.context(1);
  Socket subscriber = context.socket(ZMQ.SUB);
  subscriber.connect("tcp://localhost:5556");
        subscriber.subscribe("B".getBytes());
        while (!Thread.currentThread ().isInterrupted ()) {
            // Read envelope with address
            String address = subscriber.recvStr ();
            // Read message contents
            String contents = subscriber.recvStr ();
            System.out.println(address + " : " + contents);
        }
        subscriber.close ();
        context.term ();
}

void draw() {
}

There are no errors at all. Changing object names or removing an import an error message is promptly shown.

The code executes showing me the standard empty 100x100 window, but it doesn't work.

I read on the editor console: No library found for org.zeromq.ZMQ (twice, so I assume one is for Context, the other for Socket)

I imagine it has to do with some paths or package structure.

About /META-INF/MANIFEST.MF, its content is:

Manifest-Version: 1.0
Created-By: 1.8.0_121 (Oracle Corporation)

Any idea?

Tagged:

Answers

  • Answer ✓

    Solution found. This works:

    import org.zeromq.ZMQ;
    
    ZMQ.Socket subscriber;
    
    void settings() {
      ZMQ.Context context = ZMQ.context(1);
      subscriber = context.socket(ZMQ.SUB);
      subscriber.connect("tcp://127.0.0.1:5556");
      String filter="";
      subscriber.subscribe(filter.getBytes());
    }
    
    
    void setup() {      
    }
    
    void draw() {
      String rcv = subscriber.recvStr();
      println(rcv);
    }
    
Sign In or Register to comment.