简体   繁体   中英

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

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)

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

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 . 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. 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 '.').

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM