简体   繁体   English

AWS EC2 java 运行错误:无法找到或加载主 class

[英]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:我是 AWS EC2 的新手,我想通过执行如下所示的run.sh脚本在 EC2 实例中运行 java class MyServer

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

MyServer.java 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我通过安装编译器yum install java-devel然后javac MyServer.java编译了代码

The instance's current work directory is /home/ec2-user , and I have MyServer.class and run.sh in this folder.实例的当前工作目录是/home/ec2-user ,我在这个文件夹中有MyServer.classrun.sh

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)当我在实例中执行sh run.sh时,我收到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.我试图通过在 .sh 脚本中使用不同的 class 名称来解决它,即server.MyServerMyServer.class但它们都不起作用。

Change your shell script to something like this:将您的 shell 脚本更改为以下内容:

#!/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.否则,当 shell 解释器到达java MyServer时,当前目录已更改回原始当前目录。

The problem is that you have a package called server and you are ignoring it in your bash script.问题是您有一个名为服务器的 package 并且您在 bash 脚本中忽略了它。 Check whether /home/ec2-user/ have got the folder server or not.检查/home/ec2-user/是否有文件夹服务器 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.接下来,用java server.MyClass修改您的脚本,而不进入服务器文件夹(包),您应该能够成功执行它。

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:这是用于编译和运行的 shell 代码:

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.注意:标志-d用于可以省略的目标文件夹。

After removing package server;移除package server; from the code, it works.从代码来看,它有效。 It seems that the package was added by java automatically.似乎 package 是由 java 自动添加的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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