Java classpath, which can correct the cognition that I have been wrong

Source: Internet
Author: User

How to call a class packaged in jar in CLI using Java tool (Java command in JDK), I think most people can give a general solution:

Java-classpath XXXXX com. Test. classa

The answer is too general. If this class still depends on several jar packages and these jar packages, the jar packages including classa are all stored in the path/root/lib/. What should the content of classpath be?

The answer I gave me is-classpath =/root/lib, which is incorrect.

This error makes me puzzled. In my memory, classpath only needs to be specified to the folder path. Why?

Quick query of the official Oracle documents. Well, it does correct my previous incorrect ideas:

Class paths to the. jar,. Zip or. class files. Each classpath shoshould end with a filename or directory depending on what you are setting the class path:

    • For a. jar or. ZIP file that contains. class files, the class path ends with the name of the. Zip or. jar file.
    • For. class files in an unnamed package, the class path ends with the directory that contains the. class files.
    • For. class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name)

This is an official document. What does this mean:

It means that classpath is generally a file path or a folder path. What is actually related to the direction of classpath:

1. For the. jar and. Zip (. class can also be compressed into a zip package. I heard it for the first time), classpath should refer to these files, that is, classpath is the file path.

2. For the class file without the package path, you can specify classpath to the parent directory of the. Class file.

3. for a class file with a package path, classpath must be specified to the parent directory at the top (Root) of the package path, that is, there is ., the path of the class is/root/lib/classes/COM/test/classa. in class, the package path is com. test. classa, then the classpath should be/root/lib/classes.

This end completely explains my previous errors. For the question that was originally raised, the correct one should be classpath =/root/lib/xxx. jar; XXX. jar; XXX. jar...

This problem shows that I do not have a thorough understanding of the underlying knowledge of Java, and I am too dependent on IDE to overcome this problem.

The write is messy for future reference.

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.