Java-jar Classpath Experience

Source: Internet
Author: User
Tags log4j

source:http://sddhn.blog.163.com/blog/static/128187792011102454152790/


If a single jar file is not referenced to another jar file, and Main-class is specified, you can run this way:
Java-jar Hello.jar
What to do if the executing JAR file references other jar files.
Here Hello.jar the log4j log.
JAVA-CP Log4j-1.2.14.jar-jar Hello.jar
This can be taken for granted, and in practice there will be classnotfoundexception.
Because the-jar option is used, the environment variable CLASSPATH and all classpath specified on the command line are ignored by the JVM

The correct mode of operation is as follows:
1,
JAVA-CP Lib\log4j-1.2.14.jar;hello.jar Com.dhn.Hello (Com.dhn.Hello main Class)
separated by semicolons (;) between multiple jars under Windows, and finally you need to specify the complete main class name in the Run JAR file
2,
Java-jar Hello.jar
However, you need to modify the MANIFEST.MF in the Hello.jar to specify the other jars that are needed at run time through the Class-path in MANIFEST.MF.
The other jars can be the current path or a subdirectory under the current path. Multiple jar files separated by spaces
Take the following MANIFEST.MF file as an example
manifest-version:1.0
Main-class:com.ibm.portalnews.entrance.main
Class-path:lib\commons-collections-3.2.jar Lib\commons-configuration-1.5.jar Lib\commons-lang-2.3.jar lib\ Commons-logging.jar Lib\dom4j-1.6.1.jar Lib\jaxen-1.1-beta-7.jar Lib\jdom.jar Lib\log4j-1.2.14.jar

Where manifest-version represents the version number, which is typically generated automatically by IDE tools such as Eclipse
Main-class is the main class of the jar file, the entry of the program
Class-path Specify the desired jar, multiple jars must be on one line, multiple jars separated by spaces, and if the referenced jar is in the subdirectory of the current directory, Windows uses \ to split, Linux under/split
The file must have a blank space after the colon, or there will be an error
The last line of the file must be a carriage return line break, or there will be an error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.