简体   繁体   中英

Run an executable jar throw ClassNotFoundException

The directories in my jar files are like this:

in remote.jar

 -com
 -lib
 -META-INF

under META-INF is the file MANIFEST.MF :

Manifest-Version: 1.0
Class-Path: lib/*
Main-Class: com.xx.xx.Main

and all jars are under lib folder.

From the command line when i run java -jar remote.jar it gives me ClassNotFoundException , the class not found is in one of the jars under lib folder. If the main function does not reference any class in lib, the command line run with no problem.

My question is why it can not find the classes in jars under lib.

EDIT:

No nested folders under folder lib

The problem is that the Class-Path property in the manifest doesn't work how you think it does. See http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.

The simplest solution is to leave the classpath jars outside of your jar and to create a little batch file or shell script to construct the classpath and call your jar.

I am afraid setting your MANIFEST Class-path will not let you successfully load classes from nested jars (ie within your jar as stated in the link from Ian's answer).
If you want to package single executable jar, your best options would be :

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