简体   繁体   中英

AWS EC2 java run error: Could not find or load main class

I'm new to the AWS EC2, and I want to run a java class MyServer in an EC2 instance by executing a run.sh script that looks like this:

#!/bin/sh
cd /home/ec2-user/
java MyServer

MyServer.java

package server;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Random;

public class MyServer {
    public static void main(String[] args) throws IOException {
        String jokes[] = {"j1", "j2", "j3"};

        ServerSocket socket = new ServerSocket(9000);
        while(true){
            Socket s = socket.accept();
            PrintWriter print = new PrintWriter(s.getOutputStream(), true);
            String ip = (InetAddress.getLocalHost().getHostAddress());
            print.println(ip+jokes[(int)(Math.random()*(jokes.length-1))]);
            s.close();
            print.close();
        }
    }
}

I compiled the code by installing the compiler yum install java-devel and then javac MyServer.java

The instance's current work directory is /home/ec2-user , and I have MyServer.class and run.sh in this folder.

When I execute sh run.sh in the instance, I received Error: Could not find or load main class MyServer Caused by: java.lang.NoClassDefFoundError: server/MyServer (wrong name: MyServer)

I tried to solve it by using different class names in the.sh script, ie server.MyServer , MyServer.class but none of them works.

Change your shell script to something like this:

#!/bin/sh


(cd /home/ec2-user/ && java MyServer)

Otherwise, by the time the shell interpreter reaches java MyServer the current directory has changed back to the original current directory.

The problem is that you have a package called server and you are ignoring it in your bash script. Check whether /home/ec2-user/ have got the folder server or not. If compilation is successful then it will have it. Next, modify your script with java server.MyClass without entering into server folder(package), and you shall be able to execute it successfully.

I have been able to execute this code on my local system:

package server;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Random;

public class MyServer {
    public static void main(String[] args) throws IOException {
        String jokes[] = {"j1", "j2", "j3"};

        System.out.println("Server Started!");

        ServerSocket socket = new ServerSocket(9000);
        while(true){
            Socket s = socket.accept();
            PrintWriter print = new PrintWriter(s.getOutputStream(), true);
            String ip = (InetAddress.getLocalHost().getHostAddress());
            print.println(ip+jokes[(int)(Math.random()*(jokes.length-1))]);
            s.close();
            print.close();
        }
    }
}

This is the shell code for compiling and running:

saad@saadsap:~/java_barebone$ javac server/* -d out/
saad@saadsap:~/java_barebone$ cd out/
saad@saadsap:~/java_barebone/out$ java server.MyServer 
Server Started!

Note: Flag -d is for destination folder which can be omitted.

After removing package server; from the code, it works. It seems that the package was added by java automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM