• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts on classpath  RSS feed

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am having difficulties in understanding Classpath. What is the use of it? Suppose i set classpath for jdk. , How do i set classpath for third party class library?

I am using Fedora 8 Linux.

Thanks in advanced.

Green Horn.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the last full virus scan on my computer, I have 17,615,764 files on my computer in 840,426 different directories. If I asked you to find the file MyJavaApplication but didn't tell you where it was, how would you find it? You'd just have to start looking at every single one of my 17+ Million files until you found it. How long would that take? And what if I had multiple files named MyJavaApplication? How would you know which one was the one I wanted? How much easier would it be if I told you to look in five specific directories for it?

Well if I asked my computer -- or the JDK -- to find the file MyJavaApplication without telling it where it exists, it would have to search every file on my system. Computers are faster than us, but that's still a lot of files to look through and could take my computer several minutes to several hours to find. And it still won't know which of the 15 different MyJavaApplication files was the right one. Now compound that by the fact that I might need 25 different files in order for my program to run. Now how long will it take for my computer to find all 25? Which are the right ones? I don't want to wait more than a second for my program to start. So like you, my computer can find the necessary files faster if I tell it what specific directories, or paths, to look in for the files. I do this using the classpath. I am telling the JDK that all the classes (i.e. the code) needed to run my application can be found in following directory or directories. That is what a classpath does.

As to how to set it for a third party library... list the the jar file for that library, or the root directory of the library's package, in the classpath. For example:



You can also set the classpath using an environment variable. The Java Tutorial explains how to do such here.
 
Philip Zac
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark for beautiful explanation. I will try to do that .

Once again thanks.

Green Horn.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is a nice explanation. But 99.9% of programs discussed here on beginners don't use external files, so don't require a classpath setting anyway.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!