具体来说,我正在尝试使用Maven执行以下Amazon Web Services DynamoDB示例代码:
像这样...
c4b301bb8ad9:myapp StackOverflowUser$ pwd
/Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp
c4b301bb8ad9:myapp StackOverflowUser$ ./../mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myapp ---
[INFO] Surefire report directory: /Users/StackOverflowUser/Downloads/apache-maven-3.5.2/bin/myapp/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.example.basicapp.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.006 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.557 s
[INFO] Finished at: 2018-01-20T13:37:59-05:00
[INFO] Final Memory: 12M/225M
[INFO] ------------------------------------------------------------------------
但是当我尝试运行它时,我收到了这个错误......
c4b301bb8ad9:myapp StackOverflowUser$ java -cp target/myapp-1.0-SNAPSHOT.jar org.example.basicapp.MoviesCreateTable
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/services/dynamodbv2/document/Table
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.services.dynamodbv2.document.Table
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
c4b301bb8ad9:myapp StackOverflowUser$
我正在运行的Java版本是......
StackOverflowUser$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
我正在使用此Mac版本运行this jar file ...
我尝试添加到pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.example.basicapp.MoviesCreateTable</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
然后:
$ ./../mvn clean compile assembly:single
但是当我跑步时,我得到了:
$ java -cp target/myapp-1.0-SNAPSHOT.jar org.example.basicapp.MoviesCreateTable
Error: Could not find or load main class org.example.basicapp.MoviesCreateTable
答案 0 :(得分:1)
类加载器无法加载要导入的类(com/amazonaws/services/dynamodbv2/document/Table
。
您可以通过运行unzip -l myapp-1.0-SNAPSHOT.jar
它丢失了,因为你的最终jar工件被打包而没有它的依赖(aws-java-sdk
)。
请检查是否可以从远程存储库(在本例中为maven central)访问此依赖项,并将其下载到本地.m2
存储库。
您可以使用maven-assembly-plugin明确地包含它:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.example.basicapp.MoviesCreateTable</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
然后运行mvn clean compile assembly:single
打包它。
将创建一个新的jar - myapp-1.0-SNAPSHOT-jar-with-dependencies.jar
(与原始jar一起),现在你不必指定主类(你已经在插件配置中做了它并将它设置为你在jar清单上,只需运行java -jar target/myapp-1.0-SNAPSHOT-jar-with-dependencies.jar