• Post Reply Bookmark Topic Watch Topic
  • New Topic

What exactly is a classpath in java? Head First Java 2nd Edition  RSS feed

 
John Drulo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading the chapter on deploying the code and came across this thing called class path. I googled it and found many resources , but found the resources to be arcane and difficult to understand.

Can somebody please give me a Head First version of:
1) What a class path is exactly?
2) Given that I am nearing the end of the Head First Book, how do I google and figure out such arcane stuff on my own, without having to pester you guys all the time. Like , how would you guys go about finding what a class path is if you hadn't a clue about it?

Yours Gratefully,
John
 
Jacob Gmur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

http://en.wikipedia.org/wiki/Classpath_%28Java%29

If you're using Windows you can go trough the Control Panel to find Enviroment Variables and set CLASSPATH
For example - mine CLASSPATH is:
.;C:\Java Projects\LIB


The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)

The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

To check whether CLASSPATH is set on Microsoft Windows execute the following:

C:> echo %CLASSPATH%

http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Do you know what is package?

EXAMPLE
I've got a package:
pl\corso\simple
Inside path:
C:\Java Projects\Lib\pl\corso\simple
Inside directory simple I've got public class called List.java which belongs to pl.corso.simple package




and I've got a public class outside the package which uses pl.corso.simple package (exacly uses classt List from package)



JAVAC knows that he have to search for package inside C:\Java Projects\Lib <-- because i allready set the classpath
import pl.corso.simple.*; <- it is a place where javac have to search for the List class, if i do not set the classpath or do not import the package how the compiler could know where to search for class that I want to use

You can make lots of packages with lots of classes and use them.
you should read some about Access Control.

Correct me someone if im wrong somewhere
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Drulo wrote:I was reading the chapter on deploying the code and came across this thing called class path.

Are you sure? Programming requires you to be VERY precise. You should get in the habit now of always saying exactly what you mean. Did the book refer to "class path" or "classpath"?

John Drulo wrote:1) What a class path is exactly?

The "classpath" is an environment variable that programs can use. It basically consists of a bunch of directories. The programs then search each of those directories when looking for stuff - and as the name implies, Java uses it to find class files.

John Drulo wrote:2) Given that I am nearing the end of the Head First Book, how do I google and figure out such arcane stuff on my own, without having to pester you guys all the time. Like , how would you guys go about finding what a class path is if you hadn't a clue about it?

I would google "classpath". The very first hit I see is the Wikipedia page titled Classpath (Java)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!