如何为微机器人添加外部罐子?



我正在尝试将外部jar添加到Webots项目中。在 IntelliJ 中,我可以只做项目结构 ->模块 ->依赖项 ->添加,以添加外部 jar。如何在微机器人中做到这一点?我试图改变类路径,但没有成功。

我收到此错误:socket.java:1: error: package org.java_websocket.client does not exist,即使我的计算机上有罐子。

编辑以回应奥利维尔:

[environment variables with paths]
CLASSPATH = ../jars/Java-WebSocket-1.3.8.jar
JAVA_LIBRARY_PATH = ../jars
[java]
COMMAND = javaw.exe
OPTIONS = -Xms6144k

我已经添加了上面的代码,但这不起作用。

另外,我可以为StackOverflow提供我的代码。在这里:

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

和错误:

javac -Xlint -classpath "C:UsersuserAppDataLocalProgramsWebotslibcontrollerjavaController.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:UsersuserDocumentsmy_project3controllerssocketjavac.20200527_102128.args
Nothing to be done for build targets.

如您所见,我收到覆盖错误,因为 Webots 找不到保存方法/函数的 jar。

您应该在机器人控制器的runtime.ini文件中定义CLASSPATH变量,如下所述:

; 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

最新更新