HumorLogic
2021-08-13 16:53
采纳率: 100%
浏览 193

Java Maven项目运行出现Error: Could not find or load main class

刚开始研究Java后端,在用maven测试websocket,用的是 Java WebSockets A barebones WebSocket client and server implementation written 100% in Java - 1.5.2 - a Java package on Maven - Libraries.io https://libraries.io/maven/org.java-websocket:Java-WebSocket

主要代码:

//Server.java file
package com.my.app;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class Server extends WebSocketServer {

    public Server() throws UnknownHostException {
    }

    public Server(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
        System.out.println("websocket Server start at port: " + port);
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake clientHandshake) {
        System.out.println("new connection ===" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
    }

 /**
*省略代码
*/

    @Override
    public void onMessage(WebSocket conn, String message) {
        System.out.println("you have a new message: " + message);
        // 向客户端发送消息
        conn.send(message);
    }


     /**
     * 启动服务端
     * @param args
     * @throws UnknownHostException
     */
    public static void main(String[] args) throws UnknownHostException  {
            new Server(9996).start();     
        }
    }

}

<!-- pom.xml文件增加的依赖 -->
    <dependency>
      <groupId>org.java-websocket</groupId>
      <artifactId>Java-WebSocket</artifactId>
      <version>1.5.1</version>
    </dependency>

这个代码在windows本地,使用vscode可以运行:

img
把工程弄到 centos上,打包正常,运行时出现:

Error: Could not find or load main class com.my.app.Server
如图:

img

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

5条回答 默认 最新

相关推荐 更多相似问题