Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When importing a java library class from jar, is this considered static linking? or dynamic?

say I have jcifs-1.3.14.jar in my lib folder, and I have a class that is importing from the library and uses the classes like:

import jcifs.smb.*;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, 
                                                                 user, 
                                                                 pass);
SmbFile file = new SmbFile(path, auth);
// do some operations with the file here

When using the library in this fashion is it considered to be: A) Static Linking OR B) Dynamic Linking OR C) something else?

like image 751
Jim Ford Avatar asked Jan 26 '11 14:01

Jim Ford


People also ask

Is Java statically or dynamically linked?

Java is dynamically linked, has the slogan of "Code once, run everywhere", but it still requires JRE to run.

What is the difference between static and dynamically linked libraries?

The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while, in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.

Can Java use dynamic libraries?

System provides two methods that allow your Java applications to load dynamic libraries: load() and loadLibrary() . Note: Applets are not allowed to load dynamic libraries. A call to either load() or loadLibrary() from within an applet will result in a SecurityException.

What is statically-linked and dynamically linked libraries?

Statically-linked files are 'locked' to the executable at link time so they never change. A dynamically linked file referenced by an executable can change just by replacing the file on the disk. This allows updates to functionality without having to re-link the code; the loader re-links every time you run it.


3 Answers

If you are looking for information about applying various software licenses on Java programs, then searching Google for <license name> Java usually results in a useful hit.

E.g for LGPL Java, this is the first hit. In this particular case, the bottom line is:

Applications which link to LGPL libraries need not be released under the LGPL. Applications need only follow the requirements in section 6 of the LGPL: allow new versions of the library to be linked with the application; and allow reverse engineering to debug this.

I.e. as long as the library is provided in a separate JAR file that can be easily replaced, LGPL allows it.

PS: I Am Not A Lawyer! If in doubt, consult one. As a matter of fact, depending on where you live, it might make sense to consult one regardless if you are in doubt or not.

like image 182
thkala Avatar answered Oct 02 '22 12:10

thkala


Static vs dynamic as in C++ doesn't exist in Java. All class get loaded into JVM as they are referenced, so you'd want to think that all imports (this includes reflections) in Java are dynamic.

And that .* is bad because of the naming and class discovery conflicts that it might incur, nothing to do with class referencing.

like image 28
Ilya Saunkin Avatar answered Oct 02 '22 11:10

Ilya Saunkin


Well, you don't compile the code from library into your java classes. Your compiled classes refere the classes from other library by name. When need, the class is loaded by class loader. It's more similar to dynamic linking.

From licencing point of view - f.g. LGPL licence, it should be considered as dynamic linking. I've never heard of any law proceeding in that case (though I've searched for it), but it is high propable, I'm looking forward to it, because many developers are a bit anxious about it.

like image 23
FolksLord Avatar answered Oct 02 '22 11:10

FolksLord