I'm writing a small Java app that gets launched locally, but spawns EC2 instances. The class that I'm trying to run is Manager, which I wrapped into a JAR using a manifest file that indicates the code's entry point (Manager.main()), and then compiling the "Artifact" with IntelliJ.
When ruing the method from within IntelliJ everything works perfectly, but when trying to launch it in an EC2 instance (vis ssh), I get the following error:
[ec2-user@ip-172-31-61-121 ~]$ java -cp . -jar Manager.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/AmazonServiceException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.AmazonServiceException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
I also tried to manually set the CLASSPATH environment variable to the current working directory (/home/ec2-user), but this essentially yields the same outcome (notice that this time I didn't specifically set the class path via the execution command):
[ec2-user@ip-172-31-61-121 ~]$ java -jar Manager.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/AmazonServiceException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.AmazonServiceException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Mind you that there's no credentials issue, I've verified this by ruing the class locally (I'm using IAM roles).
It's pretty clear that the error is caused because the required classes from the exteal JARs aren't found during the loading and setup of the program from the command line, and that IntelliJ does some behind-the-scenes mumbo-jumbo that I'm unaware of.
Just to make the app's program flow clear:
- A local class is run
- The local class spawns a single EC2 instance for the Manager class
- The manager class spawns multiple EC2 instances for workers that do some information analysis, and report the results back to the Manager via SQS queues.
...and if it is of any importance, my local machine uses Java 1.8.65, and the EC2 instance uses Java 1.8.73.
