How does Java import work?
Asked Answered
S

5

66

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>
Synthesize answered 27/9, 2012 at 11:30 Comment(2)
possible duplicate of How is import done in Java?Leeuwarden
I read this thread but the thread don't answer my questionSynthesize
P
97

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.

Parlin answered 27/9, 2012 at 11:53 Comment(5)
And when a external package is called in my import, like google (look at my imports). What will be done ? I need a free internet access ?Synthesize
It seems your archives must be put in the same location pointed to by codebase, and referenced by simple names myjar.jar and foobar.jar. See this docParlin
I tryied put the <applet> tag like you said, but doesn't work. You can saw my code at stackoverflow.com/questions/12604767/…Synthesize
Where is $CLASSPATH defined? Can I change it? Does each Java application have its own $CLASSPATH?Hysteresis
I think this is not really correct: If it can't find the class, a ClassNotFoundException is thrown. If the class loader doesn't find a class, it will throw NoClassDefFoundErrorVenuti
G
37

Java's import statement is pure syntactic 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 statements when you always specify the full qualified name of classes. Like this line doesn't need any 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();
Greene answered 27/9, 2012 at 12:8 Comment(1)
Good for using the term "syntactical sugar"Nutter
A
3

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.

Aton answered 27/9, 2012 at 11:43 Comment(2)
What happens, is that my applet stop in a line where I call a method that exist in a import static com.googlecode.javacv.jna.highgui.cvCreateCameraCapture;. But localhost works fine, I got this problem just when I hot the jar file in my server. So, if the applet stops in line where call a method that exists in this imported package, maybe a internet access rule can resolve ?Synthesize
Post your object tag and directory structure. Usually applets have access to server they come fromAton
L
2

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.

Leeuwarden answered 27/9, 2012 at 11:40 Comment(0)
A
0

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: Easiest way to merge a release into one JAR file

Adelia answered 27/9, 2012 at 11:40 Comment(4)
I host my jar file in my server, so I need to edit the classpath of my server to all users access the applet without problem ?Synthesize
No, the applet will actually run on the client side. The client's classpath is the important one.Adelia
So I think the problem is not in my classpath, because in localhost the applet runs normally. But when I put the jar file in my server, the applet runs until found a method that are in a imported class like the import google. Don't generate error.Synthesize
Try to specify the location of the missing .jar file in the archive tag of your applet (the html code you updated).Adelia

© 2022 - 2024 — McMap. All rights reserved.