How does Java import work?

Java

Java Problem Overview


I would like to know how the import statement works.

I'm asking this because I have the following imports in my project:

import static com.googlecode.javacv.jna.highgui.cvCreateCameraCapture;
import static com.googlecode.javacv.jna.highgui.cvGrabFrame;
import static com.googlecode.javacv.jna.highgui.cvReleaseCapture;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.colorfulwolf.webcamapplet.gui.ImagePanel;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.jna.cxcore.IplImage;

I don't have these packages in my project, so, how will this be imported?

If I create a JAR file with all my classes, my server where will host this JAR file, has to be free Internet access to get these package?

I got some problem in my Applet that has these imports, and I'm asking this question, to understand if can be an Internet rule.

<applet code="com.colorfulwolf.webcamapplet.WebcamApplet"
archive="http://san.redenetimoveis.com/teste.jar, http://san.redenetimoveis.com/core.jar, http://san.redenetimoveis.com/javacv.jar, http://san.redenetimoveis.com/javase.jar, http://san.redenetimoveis.com/jna.jar, http://san.redenetimoveis.com/customizer.jar, http://san.redenetimoveis.com/jmf.jar, http://san.redenetimoveis.com/mediaplayer.jar, http://san.redenetimoveis.com/multiplayer.jar, http://san.redenetimoveis.com/sound.jar"
    height="550" width="550">
</applet>

Java Solutions


Solution 1 - Java

In dynamic languages, when the interpreter imports, it simply reads a file and evaluates it.

In C, external libraries are located by the linker at compile time to build the final object if the library is statically compiled, while for dynamic libraries a smaller version of the linker is called at runtime which remaps addresses and so makes code in the library available to the executable.

In Java, import is simply used by the compiler to let you name your classes by their unqualified name, let's say String instead of java.lang.String. You don't really need to import java.lang.* because the compiler does it by default. However this mechanism is just to save you some typing. Types in Java are fully qualified class names, so a String is really a java.lang.String object when the code is run. Packages are intended to prevent name clashes and allow two classes to have the same simple name, instead of relying on the old C convention of prefixing types like this. java_lang_String. This is called namespacing.

BTW, in Java there's the static import construct, which allows to further save typing if you use lots of constants from a certain class. In a compilation unit (a .java file) which declares

import static java.lang.Math.*;

you can use the constant PI in your code, instead of referencing it through Math.PI, and the method cos() instead of Math.cos(). So for example you can write

double r = cos(PI * theta);

Once you understand that classes are always referenced by their fully qualified name in the final bytecode, you must understand how the class code is actually loaded. This happens the first time an object of that class is created, or the first time a static member of the class is accessed. At this time, the ClassLoader tries to locate the class and instantiate it. If it can't find the class a NoClassDefFoundError is thrown (or a a ClassNotFoundException if the class is searched programmatically). To locate the class, the ClassLoader usually checks the paths listed in the $CLASSPATH environment variable.

To solve your problem, it seems you need an applet element like this

<applet
  codebase = "http://san.redenetimoveis.com"
  archive="test.jar, core.jar"
  code="com.colorfulwolf.webcamapplet.WebcamApplet"      
  width="550" height="550" >

BTW, you don't need to import the archives in the standard JRE.

Solution 2 - Java

Java's import statement is pure syntactical sugar. import is only evaluated at compile time to indicate to the compiler where to find the names in the code.

You may live without any import statement when you always specify the full qualified name of classes. Like this line needs no import statement at all:

javax.swing.JButton but = new  javax.swing.JButton();

The import statement will make your code more readable like this:

import javax.swing.*;

JButton but = new JButton();

Solution 3 - Java

Import in Java does not work at all, as it is evaluated at compile time only. (Treat it as shortcuts so you do not have to write fully qualified class names). At runtime there is no import at all, just FQCNs.

At runtime it is necessary that all classes you have referenced can be found by classloaders. (classloader infrastructure is sometimes dark magic and highly dependent on environment.) In case of an applet you will have to rig up your HTML tag properly and also provide necessary JAR archives on your server.

PS: Matching at runtime is done via qualified class names - class found under this name is not necessarily the same or compatible with class you have compiled against.

Solution 4 - Java

javac (or java during runtime) looks for the classes being imported in the classpath. If they are not there in the classpath then classnotfound exceptions are thrown.

classpath is just like the path variable in a shell, which is used by the shell to find a command or executable.

Entire directories or individual jar files can be put in the classpath. Also, yes a classpath can perhaps include a path which is not local but is somewhere on the internet. Please read more about classpath to resolve your doubts.

Solution 5 - Java

The classes which you are importing have to be on the classpath. So either the users of your Applet have to have the libraries in the right place or you simply provide those libraries by including them in your jar file. For example like this: https://stackoverflow.com/questions/81260/java-easiest-way-to-merge-a-release-into-one-jar-file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucas_SantosView Question on Stackoverflow
Solution 1 - JavaRaffaeleView Answer on Stackoverflow
Solution 2 - JavaPeterMmmView Answer on Stackoverflow
Solution 3 - JavaKonstantin PribludaView Answer on Stackoverflow
Solution 4 - JavaCuriousView Answer on Stackoverflow
Solution 5 - JavaKaiView Answer on Stackoverflow