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.