How Java Import Works - Stack Overflow PDF
How Java Import Works - Stack Overflow PDF
How Java Import Works - Stack Overflow PDF
xDismiss
JointheStackOverflowCommunity
Signup
Howjavaimportworks
importstaticcom.googlecode.javacv.jna.highgui.cvCreateCameraCapture;
importstaticcom.googlecode.javacv.jna.highgui.cvGrabFrame;
importstaticcom.googlecode.javacv.jna.highgui.cvReleaseCapture;
importcom.googlecode.javacv.CanvasFrame;
importcom.googlecode.javacv.FrameGrabber;
importcom.colorfulwolf.webcamapplet.gui.ImagePanel;
importcom.googlecode.javacv.OpenCVFrameGrabber;
importcom.googlecode.javacv.jna.cxcore.IplImage;
UPDATE
<appletcode="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
editedSep27'12at11:55 askedSep27'12at11:30
Lucas_Santos
2,120 8 43 87
5Answers
http://stackoverflow.com/questions/12620369/howjavaimportworks 1/3
5/19/2017 HowjavaimportworksStackOverflow
InC,externallibrariesarelocatedbythelinkeratcompiletimetobuildthefinalobjectifthelibrary
isstaticallycompiled,whilefordynamiclibraries asmallerversionofthelinkeriscalledatruntime
whichremapsaddressesandsomakescodeinthelibraryavailabletotheexecutable.
BTW,inJavathere'sthestaticimportconstruct,whichallowstofurthersavetypingifyouuse
lotsofconstantsfromacertainclass.Inacompilationunit(a.javafile)whichdeclares
importstaticjava.lang.Math.*;
doubler=cos(PI*theta);
Onceyouunderstandthatclassesarealwaysreferencedbytheirfullyqualifiednameinthefinal
bytecode,youmustunderstandhowtheclasscodeisactuallyloaded.Thishappensthefirsttime
anobjectofthatclassiscreated,orthefirsttimeastaticmemberoftheclassisaccessed.At
thistime,the ClassLoader triestolocatetheclassandinstantiateit.Ifitcan'tfindtheclass,a
ClassNotFoundException isthrown.Tolocatetheclass,the ClassLoader usuallychecksthepaths
listedinthe $CLASSPATH environmentvariable.
<applet
codebase="http://san.redenetimoveis.com"
archive="test.jar,core.jar"
code="com.colorfulwolf.webcamapplet.WebcamApplet"
width="550"height="550">
BTW,youdon'tneedtoimportthearchivesinthestandardJRE.
editedSep27'12at12:32 answeredSep27'12at11:53
Raffaele
16k 5 27 64
Andwhenaexternalpackageiscalledinmyimport,likegoogle(lookatmyimports).Whatwillbedone?I
needafreeinternetaccess? Lucas_Santos Sep27'12at11:58
2 Whereis$CLASSPATHdefined?CanIchangeit?DoeseachJavaapplicationhaveitsown
$CLASSPATH?NikoBellic Jul26'14at22:59
Java'simportstatementispuresyntacticalsugar.importisonlyevaluatedatcompiletimeto
indicatetothecompilerwheretofindthenames inthecode.
Youmaylivewithoutanyimportstatementwhenyoualwaysspecifythefullqualifiednameof
classes.Likethislineneedsnoimportstatementatall:
javax.swing.JButtonbut=newjavax.swing.JButton();
Theimportstatementwillmakeyourcodemorereadablelikethis:
importjavax.swing.*
JButtonbut=newJButton();
answeredSep27'12at12:08
PeterMmm
16.1k 8 44 91
2 Goodforusingtheterm"syntacticalsugar"GabNov21'16at9:39
http://stackoverflow.com/questions/12620369/howjavaimportworks 2/3
5/19/2017 HowjavaimportworksStackOverflow
Importinjavadoesnotworkatall,asitisevaluatedatcompiletimeonly.(treatitasshortcutsso
youdonothavetowriteFullyQualifiedClassnames)Attheruntimethereisnoimportatall,just
FQCNs.
Attheruntimeitisnecessarythatallclassesyouhavereferencedcanbefoundbyclassloaders.
(classloaderinfrastructureissometimesdarkmagicandhighlydependentonenvironment)In
caseofappletyouwillhavetorigupyourhtmltagproperlzandalsoprovidenecessaryjar
archivesonyourserver
PS:matchingattheruntimeisdoneviaqualifiedclassnamesclassfoundunderthisnameis
notnecessarilythesameorcompatiblewithclassyouhavecompiledagainst.
answeredSep27'12at11:43
KonstantinPribluda
10.5k 1 18 29
Whathappens,isthatmyappletstopinalinewhereIcallamethodthatexistina importstatic
com.googlecode.javacv.jna.highgui.cvCreateCameraCapture; .But localhost worksfine,Igotthis
problemjustwhenIhotthe jar fileinmyserver.So,ifthe applet stopsinlinewherecallamethodthat
existsinthisimportedpackage,maybeainternetaccessrulecanresolve? Lucas_Santos Sep27'12
at11:50
1 Postyourobjecttaganddirectorystructure.Usuallyappletshaveaccesstoservertheycomefrom
KonstantinPribludaSep27'12at11:53
answeredSep27'12at11:40
Curious
990 5 21
Theclasseswhichyouareimportinghavetobeontheclasspath.Soeithertheusersofyour
Applethavetohavethelibrariesintherightplaceoryousimplyprovidethoselibrariesby
includingtheminyourjarfile.Forexamplelikethis:Java:Easiestwaytomergeareleaseinto
onejarfile
answeredSep27'12at11:40
Kai
23.5k 8 61 89
SoIthinktheproblemisnotinmyclasspath,becauseinlocalhosttheappletrunsnormally.ButwhenIput
thejarfileinmyserver,theappletrunsuntilfoundamethodthatareinaimportedclassliketheimport
google.Don'tgenerateerror. Lucas_Santos Sep27'12at12:05
Trytospecifythelocationofthemissing.jarfileinthearchivetagofyourapplet(thehtmlcodeyou
updated).KaiSep27'12at12:10
http://stackoverflow.com/questions/12620369/howjavaimportworks 3/3