Home > Back-end >  AWS EC2 java run error: Could not find or load main class
AWS EC2 java run error: Could not find or load main class

Time:09-12

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related