• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading of classes in a source file  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I import a class or package in Java source file, does it get loaded in source file from CLASSPATH ?
Do these classes exist in JRE or JDK ?
Does this CLASSPATH for java class files automatically get set when installing ? I have set java PATH and CLASSPATH only for bin dir and compiled classes dir created by me.
So how does CLASSPATH for classes in java packages get set ?
OR

In this code, where does java.util.*; and java.io.*; get loaded from ? I have not provided any path for these two imports and I have not set any CLASSPATH for these packages either.

Below is the CLASSPATH variable value of the server I am working on.
.;D:\iw-home\iw-perl\iwperl.exe;
C:\PROGRA~1\IBM\SQLLIB\java\db2java.zip;
C:\PROGRA~1\IBM\SQLLIB\java\db2jcc.jar;
C:\PROGRA~1\IBM\SQLLIB\java\sqlj.zip;
C:\PROGRA~1\IBM\SQLLIB\java\db2jcc_license_cu.jar;
C:\PROGRA~1\IBM\SQLLIB\bin;
C:\PROGRA~1\IBM\SQLLIB\java\common.jar;

I dont see java classes set anywhere is in. So how classes get loaded in source file when import statement is inserted in source file ?

Thanks
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I import a class or package in Java source file, does it get loaded in source file from CLASSPATH? Do these classes exist in JRE or JDK ?

Classes that come with the JRE -like the java.* and javax.* package hierarchies that are part of the JRE- are found automatically. 3rd party libraries and your own classes need to be added to the classpath. Note that "CLASSPATH" is the name of an environment variable which can be used to set the classpath, but there are other methods as well, like the "-cp" option for java and javac. The JDK adds tools to the JRE -like the compiler- but no classes you would normally use.

I have set java PATH and CLASSPATH only for bin dir and compiled classes dir created by me.

Those two serve entirely different purposes. I would go so far to say that no directory should be in both the PATH and the CLASSPATH. PATH is for finding executables, not class files. CLASSPATH is for finding clases, not executables.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:When I import a class or package in Java source file, does it get loaded in source file from CLASSPATH ?
Do these classes exist in JRE or JDK ?
Does this CLASSPATH for java class files automatically get set when installing ? I have set java PATH and CLASSPATH only for bin dir and compiled classes dir created by me.
So how does CLASSPATH for classes in java packages get set ?
OR

In this code, where does java.util.*; and java.io.*; get loaded from ? I have not provided any path for these two imports and I have not set any CLASSPATH for these packages either.

Thanks
No, .class files are only loaded at runtime, when they are required by the JVM. Compile that class and display it with javap -c ClassTest You will need to do something about the Exceptions before it compiles.
The javac tool looks through the java.util and java.io packages (not sure which order) until it finds a class called File (if it finds two with the same name it will complain). Then it writes the class' fully‑qualified name in the .class file, for later use. you should see that in the printout. All standard JRE files live in a file called rt.jar which is in the Java® installation folder, and the JVM loads them automatically. Then it looks through your CLASSPATH to find the other classes it needs; if there is no CLASSPATH it defaults to looking in the current directory, imputing a CLASSPATH="." I can't remember whether the JVM disambiguates two classes with the same name. Look through your Java® installation folder for rt.jar.
[Also look for src.zip and unzip it; that has the code for many of the standard Java classes in.]

If you have set a system CLASSPATH, that usually does more harm than good, and you ought to delete it. If there is anything in your CLASSPATH which you didn't set yourself, remove what you added yourself and add "." to the beginning of the CLASSPATH.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!