Java command execution files with jar package dependencies cannot be executed

import org.apache.commons.io.FilenameUtils;

public class Test {

    public static void main(String[] args) throws InterruptedException {
        System.out.println(FilenameUtils.getExtension("a.zip"));
    }
}

as shown above, the simple code relies on the commons-io.jar package

compile command:
javac-cp commons-io-2.4.jar-d. Test.java
compiled successfully!

execute the command:
java-cp commons-io-2.4.jar Test
error:
error: the main class Test cannot be found or cannot be loaded

change to the specified directory:
java-cp. Test
error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/FilenameUtils
        at Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FilenameUtils
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

excuse me, what is the cause?

Mar.21,2021

if it is Windows:


jarmanifest.mfmain startmavenmaven:

mvn clean -U install
mvn package

if you only execute the jar program of main, you can use

under * unix.
$ java -cp $path/1.jar;2.jar;main.jar MainClass

another way is to modify the path parameter of the class-path variable in manifest.mf, probably as follows:

...
Main-Class: xxx.xxx.xxx.MainClass
Class-Path:/xxx/hello.jar,/lib/xxx/ahaha.jar
This is probably the structure of

. Add the toolkit you need to rely on to the path and then run it:

$ java -jar main-name.jar

Windows may be similar, but the details are not clear

Menu