java - JDK 1.7错误: Could not find or load main class when using command "java -cp" with multiple jars

标签 java maven classpath java-7

我创建了一个名为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/

相关文章:

java - 使用 Mockito 对 spring boot Controller 进行单元测试

Java 使用 SHA1

java - 文件未添加到 docker 工作区

java - 在耳边开始 session

c# - Java 到 C# : HashMap, 映射和队列

java - Maven 程序集插件 : add a file into a dependency jar

java - Maven 构建显示编译错误,但 Eclipse 没有

java - 在 OS X 上编译 TuxGuitar,javac 找不到包

java - ClassNotFoundException : org. ear 文件中的 jaxen.JaxenException

java - Ant 。将属性文件添加到 jar 运行时类路径