错误:无法找到或加载主类TestClient



我正在尝试用这里提供的示例代码构建一个HelloWord websocket客户端:https://github.com/Gottox/socket.io-java-client/blob/master/examples/basic/BasicExample.java

目标是连接到一个Flask服务器,使用:https://github.com/miguelgrinberg/Flask-SocketIO-Chat我用python构建的客户端工作良好,但是我还需要构建一个java客户端。

我已经看到了很多不同的解决方案,建议在编译期间添加-cp,但仍然得到相同的消息Error: Could not find or load main class TestClient我做错了什么?

我使用这个脚本来编译&运行。

#!/bin/sh
export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
/usr/lib/jvm/java-8-oracle/bin/javac TestClient.java -cp /home/erm/git/Flask-SocketIO-Chat/*.jar:*:.:./
echo "exitcode:$?"
/usr/lib/jvm/java-8-oracle/bin/java TestClient

输出如下:

exitcode:0
Error: Could not find or load main class TestClient

TestClient.java

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import org.json.JSONException;
import org.json.JSONObject;
public class TestClient implements IOCallback {
    private SocketIO socket;
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Hello, World");
        try {
            new TestClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public TestClient() throws Exception {
        socket = new SocketIO();
        socket.connect("http://127.0.0.1:5000/", this);
        // Sends a string to the server.
        socket.send("Hello Server");
        // Sends a JSON object to the server.
        socket.send(new JSONObject().put("key", "value").put("key2",
                "another value"));
        // Emits an event to the server.
        socket.emit("event", "argument1", "argument2", 13.37);
    }
    @Override
    public void onMessage(JSONObject json, IOAcknowledge ack) {
        try {
            System.out.println("Server said:" + json.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onMessage(String data, IOAcknowledge ack) {
        System.out.println("Server said: " + data);
    }
    @Override
    public void onError(SocketIOException socketIOException) {
        System.out.println("an Error occured");
        socketIOException.printStackTrace();
    }
    @Override
    public void onDisconnect() {
        System.out.println("Connection terminated.");
    }
    @Override
    public void onConnect() {
        System.out.println("Connection established");
    }
    @Override
    public void on(String event, IOAcknowledge ack, Object... args) {
        System.out.println("Server triggered event '" + event + "'");
    }
}

错误信息

Error: Could not find or load main class TestClient

表示Java启动器无法找到/加载TestClient.class类文件。

要正确加载它,类文件本身和jar文件socketio.jar都需要放在类路径上。所以请尝试

/usr/lib/jvm/java-8-oracle/bin/java -cp .:socketio.jar TestClient

/usr/lib/jvm/java-8-oracle/bin/java -cp /home/erm/git/Flask-SocketIO-Chat:/home/erm/git/Flask-SocketIO-Chat/socketio.jar TestClient

如果文件丢失,前面的答案应该有效。

但是在那之前,你能检查一下Java文件是否被编译了,因为它正在通过shell运行吗?

如果没有,请编译java文件

javac TestClient.java

如果您在TestClient.java所在的路径中或者提供完整的路径。javac /app/javafiles/TestClient.java

相关内容

  • 没有找到相关文章

最新更新