[英]JDK 1.7 Error: Could not find or load main class when using command “java -cp” with multiple jars
i have created a maven project named a-test and only a main class in it 我创建了一个名为a-test的Maven项目,并且其中只有一个主类
package com.jar.test.a;
public class AppTest
{
public static void main( String[] args )
{
System.out.println( "hi test");
}
}
package it 打包
mvn clean install
cd target
and it works as running 它可以运行
java -cp a-test-0.0.1-SNAPSHOT.jar com.jar.test.a.AppTest
or 要么
java -cp *.jar com.jar.test.a.AppTest
and it prints 它打印
hi test
but it will issue error when i want to specify a multiple jars path (i want to import another java project) 但是当我要指定多个jar路径时(我要导入另一个Java项目),它将发出错误消息
java -cp *.jar:/usr/lib/*.jar com.jar.test.a.AppTest
Error: Could not find or load main class 错误:找不到或加载主类
it will appear the same error when i have test this on macbook os x 10.10.3 and centos6 当我在Macbook OS x 10.10.3和centos6上对此进行测试时,它将出现相同的错误
java version "1.7.0_71"
I have tried this but it did not work for me. 我已经尝试过了,但是对我没有用。
You should issue: 您应该发出:
java -cp *:/usr/lib/* com.jar.test.a.AppTest
... since *
implies *.jar
. ...因为*
表示*.jar
。 See Understanding class path wildcards : 请参阅了解类路径通配符 :
Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. 类路径条目可以包含基本名称通配符*,这被认为等效于指定扩展名为.jar或.JAR的目录中所有文件的列表。 For example, the class path entry foo/* specifies all JAR files in the directory named foo. 例如,类路径条目foo / *指定目录foo中的所有JAR文件。 A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. 仅由*组成的类路径条目将扩展为当前目录中所有jar文件的列表。 Files will be considered regardless of whether or not they are hidden (that is, have names beginning with '.'). 无论文件是否隐藏(即名称以“。”开头),都将考虑它们。
Be careful with a bare single *
though, since your shell will probably want to expand it as a wildcard, in which case you can quote it. 但是,请注意不要使用单个*
,因为您的外壳可能会希望将其扩展为通配符,在这种情况下,您可以引用它。
您应该这样执行它(Unix系统):
java -cp a-test-0.0.1-SNAPSHOT.jar:lib/*:. com.jar.test.a.AppTest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.