How to add external jars to Webots?

Jeroen Hulshof :

I'm trying to add external jar to a Webots project. In IntelliJ i can just do project structure -> modules -> dependencies -> add, to add the external jar. How to do this in Webots? I've tried to change the classpath, but no success..

I get this error: socket.java:1: error: package org.java_websocket.client does not exist, eventhough i have the jars on my computer.

Edit in response to Olivier:

[environment variables with paths]
CLASSPATH = ../jars/Java-WebSocket-1.3.8.jar
JAVA_LIBRARY_PATH = ../jars

[java]
COMMAND = javaw.exe
OPTIONS = -Xms6144k

I've added the code above, but this didn't work.

Also, i could provide StackOverflow with my code. Here it is:

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;

public class socket {

  public static void main(String args[]) throws URISyntaxException, InterruptedException {


    WebSocketClient mWs = new WebSocketClient(new URI("ws://localhost:8000"), new Draft_6455()) {
      @Override
      public void onMessage(String message) {
        JSONObject obj = new JSONObject(message);
        String channel = obj.getString("channel");
      }

      @Override
      public void onOpen(ServerHandshake handshake) {
        System.out.println("opened connection");
        this.send("Connection opened");

      }

      @Override
      public void onClose(int code, String reason, boolean remote) {
        System.out.println("closed connection");
      }

      @Override
      public void onError(Exception ex) {
        ex.printStackTrace();
      }

    };

    //open websocket
    mWs.connectBlocking();
    JSONObject obj = new JSONObject();
    obj.put("event", "addChannel");
    obj.put("channel", "ok_btccny_ticker");
    String message = obj.toString();
    //send message
//    mWs.send(message);
  }
}

And the errors:

    javac -Xlint -classpath "C:\Users\user\AppData\Local\Programs\Webots\lib\controller\java\Controller.jar;;." socket.java
socket.java:1: error: package org.java_websocket.client does not exist
import org.java_websocket.client.WebSocketClient;
                                ^
socket.java:2: error: package org.java_websocket.drafts does not exist
import org.java_websocket.drafts.Draft_6455;
                                ^
socket.java:3: error: package org.java_websocket.handshake does not exist
import org.java_websocket.handshake.ServerHandshake;
                                   ^
socket.java:4: error: package org.json does not exist
import org.json.JSONObject;
               ^
socket.java:14: error: cannot find symbol
    WebSocketClient mWs = new WebSocketClient(new URI("ws://localhost:8000"), new Draft_6455()) {
    ^
  symbol:   class WebSocketClient
  location: class socket
socket.java:14: error: cannot find symbol
    WebSocketClient mWs = new WebSocketClient(new URI("ws://localhost:8000"), new Draft_6455()) {
                              ^
  symbol:   class WebSocketClient
  location: class socket
socket.java:14: error: cannot find symbol
    WebSocketClient mWs = new WebSocketClient(new URI("ws://localhost:8000"), new Draft_6455()) {
                                                                                  ^
  symbol:   class Draft_6455
  location: class socket
socket.java:22: error: cannot find symbol
      public void onOpen(ServerHandshake handshake) {
                         ^
  symbol: class ServerHandshake
socket.java:15: error: method does not override or implement a method from a supertype
      @Override
      ^
socket.java:17: error: cannot find symbol
        JSONObject obj = new JSONObject(message);
        ^
  symbol: class JSONObject
socket.java:17: error: cannot find symbol
        JSONObject obj = new JSONObject(message);
                             ^
  symbol: class JSONObject
socket.java:21: error: method does not override or implement a method from a supertype
      @Override
      ^
socket.java:24: error: cannot find symbol
        this.send("Connection opened");
            ^
  symbol: method send(String)
socket.java:28: error: method does not override or implement a method from a supertype
      @Override
      ^
socket.java:33: error: method does not override or implement a method from a supertype
      @Override
      ^
15 errors
printing javac parameters to: C:\Users\user\Documents\my_project3\controllers\socket\javac.20200527_102128.args
Nothing to be done for build targets.

As you can see, i get override errors because Webots can't find the jar that holds the methods/functions.

Olivier Michel :

You should define the CLASSPATH variable in the runtime.ini file of your robot controller as explained here:

; runtime.ini for a Java controller on Windows

[environment variables with paths]
CLASSPATH = ../lib/MyLibrary.jar
JAVA_LIBRARY_PATH = ../lib

[java]
COMMAND = javaw.exe
OPTIONS = -Xms6144k

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add external jars globally

How to add external JARs to Spring application without restarting JVM?

Flink on yarn: how to add external jars class path in local Client

What's the difference between "Add JARs" and "Add External JARs" in Eclipse?

How to add external folder that contains multiple jars into maven build for web application?

How to use external Jars in javafxports-Application

How to generate a jar with internal dependencies bundled and external dependencies as external jars?

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

Add directory to the jar which will be for insert external jars or classes

How do you add a PROTO node programmatically in Webots?

how to add files to jars META-INF

leiningen - how to add dependencies for local jars?

How to use a wildcard in the classpath to add multiple jars?

How to add lib with jars to shared folder?

How to add hive auxiliary jars to Dataproc cluster

How to know groupid and artifactid of any external jars in maven android project

CQ5 how to use external jars in custom plugins

Where to put the external jars?

Issue in PlayFramework for external Jars

WEBOTS - Write controller data to external CSV file

Maven and eclipse: a reliable way to add non-Maven or external jars to a project?

java.lang.NoClassDefFoundError when I add external JARs using Eclipse IDE

Importing external jars and Gradle error

Java classpath issue with external jars

External jars in eclipse plug-in

Adding external jars in EMR Notebooks

Adding External jars to Anypoint studio

Including external jars permanently in NetBeans

Adding external jars to ModCoderPack (MCP)