5/19/2017 HowjavaimportworksStackOverflow
xDismiss
JointheStackOverflowCommunity
Stack Overflow is a community of 7.1 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup
Howjavaimportworks
Iwouldliketoknowhowthe import works.
I'maskingthis,becauseIhavethefollowing imports inmyproject
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
[Link];
Idon'thaveinmyprojectthese package so,howthiswillbeimported?
IfIcreatea jar filewithallmyclasses,myserverwherewillhostthis jar file,hastobefreeinternetaccesstoget
these package ?
Igotsomeprobleminmy Applet thathasthese import andI'maskingthisquestion,tounderstandifcanbeainternet
rule.
UPDATE
<appletcode="[Link]"
archive="[Link]
[Link]
[Link]
[Link]
[Link]
height="550"width="550">
</applet>
java
editedSep27'12at11:55 askedSep27'12at11:30
Lucas_Santos
2,120 8 43 87
possibleduplicateofHowisimportdoneinJava? Curious Sep27'12at11:33
2 Ireadthisthreadbutthethreaddon'tanswermyquestion Lucas_Santos Sep27'12at11:35
5Answers
Indynamiclanguages,whentheinterpreter import s,itsimplyreadsafileandevaluatesit.
[Link] 1/3
5/19/2017 HowjavaimportworksStackOverflow
InC,externallibrariesarelocatedbythelinkeratcompiletimetobuildthefinalobjectifthelibrary
isstaticallycompiled,whilefordynamiclibraries asmallerversionofthelinkeriscalledatruntime
whichremapsaddressesandsomakescodeinthelibraryavailabletotheexecutable.
InJava, import issimplyusedbythecompilertoletyounameyourclassesbytheirunqualified
name,let'ssay String insteadof [Link] .Youdon'treallyneedtoimport [Link].*
[Link]
[Link],soa String isreallya [Link]
[Link]
classestohavethesamesimplename,insteadofrelyingontheoldCconventionofprefixing
typeslikethis. java_lang_String .Thisiscallednamespacing.
BTW,inJavathere'sthestaticimportconstruct,whichallowstofurthersavetypingifyouuse
[Link]([Link])whichdeclares
[Link].*;
youcanusetheconstant PI inyourcode,insteadofreferencingitthrough [Link] ,andthe
method cos() insteadof [Link]() .Soforexampleyoucanwrite
doubler=cos(PI*theta);
Onceyouunderstandthatclassesarealwaysreferencedbytheirfullyqualifiednameinthefinal
bytecode,[Link]
anobjectofthatclassiscreated,[Link]
thistime,the ClassLoader [Link]'tfindtheclass,a
ClassNotFoundException [Link],the ClassLoader usuallychecksthepaths
listedinthe $CLASSPATH environmentvariable.
Tosolveyourproblem,itseems youneedan applet elementlikethis
<applet
codebase="[Link]
archive="[Link],[Link]"
code="[Link]"
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
Itseemsyourarchivesmustbeputinthesamelocationpointedtoby codebase ,andreferencedby
simplenames [Link] and [Link] .Seethisdoc RaffaeleSep27'12at12:05
Itryiedputthe <applet> taglikeyousaid,butdoesn'[Link]
[Link]/questions/12604767/ Lucas_Santos Sep27'12at12:25
2 Whereis$CLASSPATHdefined?CanIchangeit?DoeseachJavaapplicationhaveitsown
$CLASSPATH?NikoBellic Jul26'14at22:59
Java'[Link]
indicatetothecompilerwheretofindthenames inthecode.
Youmaylivewithoutanyimportstatementwhenyoualwaysspecifythefullqualifiednameof
[Link]:
[Link]=[Link]();
Theimportstatementwillmakeyourcodemorereadablelikethis:
[Link].*
JButtonbut=newJButton();
answeredSep27'12at12:08
PeterMmm
16.1k 8 44 91
2 Goodforusingtheterm"syntacticalsugar"GabNov21'16at9:39
[Link] 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
[Link]; .But localhost worksfine,Igotthis
problemjustwhenIhotthe jar [Link],ifthe applet stopsinlinewherecallamethodthat
existsinthisimportedpackage,maybeainternetaccessrulecanresolve? Lucas_Santos Sep27'12
at11:50
1 [Link]
KonstantinPribludaSep27'12at11:53
Questionupdated,the jar arein [Link]/[Link] Lucas_Santos Sep27'12at11:56
javac (or java duringruntime)looksforthe classes beingimportedinthe classpath .Ifthey
arenotthereinthe classpath then classnotfound exceptionsarethrown.
classpath isjustlikethe path variableinashell,whichisusedbytheshelltofindacommand
orexecutable.
Entiredirectoriesorindividualjarfilescanbeputinthe classpath .Also,yesa classpath can
[Link]
aboutclasspathtoresolveyourdoubts.
answeredSep27'12at11:40
Curious
990 5 21
[Link]
Applethavetohavethelibrariesintherightplaceoryousimplyprovidethoselibrariesby
[Link]:Java:Easiestwaytomergeareleaseinto
onejarfile
answeredSep27'12at11:40
Kai
23.5k 8 61 89
Ihostmy jar fileinmyserver,soIneedtoedittheclasspathofmyservertoallusersaccesstheapplet
withoutproblem? Lucas_Santos Sep27'12at12:00
No,[Link]'sclasspathistheimportantone. Kai Sep27
'12at12:03
SoIthinktheproblemisnotinmyclasspath,[Link]
thejarfileinmyserver,theappletrunsuntilfoundamethodthatareinaimportedclassliketheimport
[Link]'tgenerateerror. Lucas_Santos Sep27'12at12:05
[Link](thehtmlcodeyou
updated).KaiSep27'12at12:10
[Link] 3/3