简体   繁体   中英

What's the difference of compile time libraries and run time libraries in java?

And what are the pro's con's of using either?

I actually saw it in Netbeans under Project Properties > Libraries for Java Applications. We have two tabs, one for compile time libraries and run time libraries, and it looks like we can add a library to either independent of each other

There is no such a thing as compile time libraries vs. runtime libraries

Perhaps you're mixing some concepts.

In Java the libraries to be used are statically validated at compile time and also validated at runtime.

For instance if you want to use IterableMap specified in the Apache Collections library. The compiler validates "at compile time" you are invoking a method that exist in that class.

But the compiler doesn't link or do much of anything with that library, you still need it at runtime. So, when your code executes, the Java runtime, searches for that class again, and invokes the method the compiler verified existed.

And that what there is.

The UI and terminology of the Libraries properties dialog is pretty confusing.

The Help button on that dialog will give you a fair bit of info.

The Compile-time library list may be a subset of the Run-time library list.

Consider this situation...

You have source code that imports classes from on a library 'widgets.jar'. The class files in widgets.jar reference symbols from the jar file 'xml.jar'. If your source code does not import classes from xml.jar, you could define the Compile-time libraries list to contain just widgets.jar.

When you try to run your project, you will probably need to include xml.jar in the Run-time libraries list to prevent ClassNotFoundException's.

Perhaps, this comes into play when you want to load a library dynamically, or check for an existence of the library and then execute the code.

During compilation, compiler needs to know what the signatures of the methods, classes etc to know if you code is correct. Hence you add the compile time library.

During runtime, JVM still needs the library to run that specific code. But you can put a logic to avoid that code by checking if the library exists, for example by doing Class.for() method. Some libraries might already exist in the system (qt.jar for example) or might not, and you can check and execute your code accordingly.

Please correct me if I am wrong.

As others have stated, you are confusing concepts. I think what you are really trying to understand is what Maven refers to as dependency scope . Sometimes you only need a dependency at compile time because you expect it to be provided at runtime, and sometimes you need it at runtime but not compile time.

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