我创建了一个名为a-test的maven项目,其中只有一个主类
package com.jar.test.a;
public class AppTest
{
public static void main( String[] args )
{
System.out.println( "hi test");
}
}
打包
mvn clean install
cd target
并且它可以运行
java -cp a-test-0.0.1-SNAPSHOT.jar com.jar.test.a.AppTest
或
java -cp *.jar com.jar.test.a.AppTest
然后打印
hi test
但是当我想指定多个jar路径(我想导入另一个java项目)时它会发出错误
java -cp *.jar:/usr/lib/*.jar com.jar.test.a.AppTest
Error: Could not find or load main class
当我在 macbook os x 10.10.3 和 centos6 上测试时,会出现同样的错误
java version "1.7.0_71"
我已经尝试过this但它对我不起作用。
最佳答案
您应该发出:
java -cp *:/usr/lib/* com.jar.test.a.AppTest
...因为 *
意味着 *.jar
。请参阅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. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. Files will be considered regardless of whether or not they are hidden (that is, have names beginning with '.').
但是请小心使用单个 *
,因为您的 shell 可能希望将其扩展为通配符,在这种情况下您可以引用它。
关于java - JDK 1.7错误: Could not find or load main class when using command "java -cp" with multiple jars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352686/