Classpath that can run jar packages in Java

Source: Internet
Author: User

< reprint annotated Source: Www.zhuowenfeng.com or blog.csdn.net/wenfengzhuo>

In Java, running an application is simple, as long as

Java XXX

You can run the program. If it's a bit more complicated, your program may rely on some third-party libraries or libraries that you implement, and you just need to

Java-classpath (-CP).: A.jar:b.jar app (Unix)
Java-classpath (-CP).; A.jar;b.jar app (Windows)

Why do you want to specify Classpath? Because do not specify CLASSPATH virtual machine where to find you want to rely on the library Ah, so do not specify Classpath, the compiler will give you a classnotfound error.

When you get to know more about Java technology and start doing some enterprise-class projects, you'll find that running a class file directly into a directory is a very low-end and no grade. So you start building your own library, making the application into a jar package, and writing your own scripting language, and then forming a high-end atmospheric application package that contains:

MyApp

-Bin

-----startup.sh

-Lib

-----App.jar

-----A.jar

-----B.jar

-conf

-----Conf.xml

-License (this is cool)

And then in the startup.sh script, you start to build the classpath and then use the

Java-classpath.: A.jar:b.jar-jar App

Run the program. Everything seems to be going so well.

Unfortunately, the program accidentally gave you a mistake, a mistake that made you very silent:

Exception in thread ' main ' java.lang.noclassdeffounderror:com/example/a/a at
	Com.example.app.App.main ( SERVER.JAVA:14)
caused By:java.lang.ClassNotFoundException:com.zhuowenfeng.util.StringUtil
	at Java.net.urlclassloader$1.run (urlclassloader.java:366) at
	Java.net.urlclassloader$1.run (URLClassLoader.java : 355) at
	java.security.AccessController.doPrivileged (Native method) at
	Java.net.URLClassLoader.findClass ( urlclassloader.java:354) at
	Java.lang.ClassLoader.loadClass (classloader.java:423) at
	sun.misc.launcher$ Appclassloader.loadclass (launcher.java:308) at
	java.lang.ClassLoader.loadClass (classloader.java:356)
	... 1 more

You are silent because you have checked n script output and see that all of the dependencies have been added to the classpath, how this could be the wrong, a bit of a reversal of your worldview.

At this point I need to tell you that the Java parameter-classpath doesn't work at all, in other words, the classpath that you've worked so hard to build is not going to work here at all.

When Java runs an executable jar package, it ignores the classpath you set and all the environment variables, and the compiler only takes your jar bundle as the root path to find the class file. This is the spontaneous jump out of a question, why do this. It may even explode a foul language, the programmer's favorite word, the letter F begins.

Sun's people (should be called Oracle now, used to say that sun) do not think of this. I'm actually exploring the question, and there are some conjectures that you can try to judge right and wrong.

Assuming that when running the executable jar package,-classpath is useful, can be happy to join a lot of dependent libraries, very convenient. Just imagine, now your application App.jar inside the class file is like this:

App.jar

-com

---example

------App.class

Then you also write another library Util.jar, as a dependent library of App.jar, which provides some kind of utility, and its structure is this:

Util.jar

-com

---example

------App.class

Graphic convenience Many jar packages are such a structure, perhaps a third party library, it also has such a class, is also such a structure. You know, when Java is running, is it the code you want to run? To know that Java's ClassLoader is very proud, loading a class, will not be loaded with the second name (full name) of the class. Therefore, the development of Java people also have difficulties.

Understand the truth, it is time to think about how to solve the problem.

One, in Java, provides a set of startup parameters:

-xbootclasspath

When you use this parameter to set up your classpath, you find that you can successfully run your jar bag, and all of you are happy. The class in the classpath specified by this parameter is loaded using the Boot ClassLoader of the JVM, the ClassLoader is the boss of all ClassLoader in the JVM, so it is not implemented by Java, but rather by using the c++/ C to achieve. So when you run your program, your dependency class will be load. Be careful not to commit the above example.

Of course it's not finished yet.

Second, the jar package in the MANIFEST.MF file

When you start using some framework to build the application, you have a deeper understanding of Java. For example, you start using spring. It's a frame you can't stop, it's almost omnipotent. Spring's annotation is ubiquitous and omnipotent, with annotations as a powerful tool, spring can do AOP, can do dependency injection, and can initialize various objects. You'll wonder how it's going to happen. I think there is a very important role in--classloader.

It is because classloader,spring can easily use annotations to manage applications. So what does this have to do with our article? It's a big relationship.

When you build an application with spring, you pack it up like the app above, and build the script. The path is then specified using-xbootclasspath. You feel foolproof, then run the script, but the program throws you a mistake:

Bean cannot be initialized ...

I think you should have known the reason. Yes, that's right, because-xbootclasspath your dependency class load into the JVM in advance so that your spring ClassLoader can no longer initialize the Component,controller that you've declared with annotation. And so on. was not nearly crashing.

But there is a solution to everything. Jar Package It provides a MANIFEST.MF file that describes the properties of this jar package and other information. Like Main-class, like Class-path, yes. This is the class-path that can solve all of the above problems and let you stop worrying about the classpath of the jar pack. You simply specify the jar package name you depend on in the MANIFEST.MF, then place the jar packages in the same level directory or subdirectory of the application jar package, and then easily run the program.

Class-path:a.jar B.jar Lib/c.jar

So here's what I want to say.

Third, there is actually a very silly method, the dependency package copy to the {Java_home}\jre\lib\ext directory, and then use the system's extension classloader to load these classes.

Would you do that? I guess not.
























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.