• Post Reply Bookmark Topic Watch Topic
  • New Topic

classpath question  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

classpath is the "class search path".Thus java
and javac commands search for sourcefile in the
classpath mentioned.
But even if I haven't mentioned .(to search
in current directory) as one of the
element of classpath then also javac and java
commands succesfully compile and run any class
file present in the current directory...why??
what I think is compiler should search for
source file in C:\Program Files\Java\jdk1.5.0_06\bin
directory and it shouldn't be able to find my
source file unless i use . as one of the classpath
elemnt i.e,C:\Program Files\Java\jdk1.5.0_06\bin;.;

By mistakely I posted the above question in a irrelevant forum
I didn't know to move question from one forum to other
please tell me how to do it....


thanks in advance
with regards
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is an appropriate forum for that question. Did you post it twice? I can find the duplicate and remove it, in that case.
Please avoid coloured text; I have removed the colours because some people find them difficult to read, and replaces the green text with underlined text.

you should not put your own files in the bin folder. Nor should you search there for .class files. The JVM is programmed to search a file called rt.jar in the /jre/lib directory, where it finds all the classes in the standard API. It does not search the bin folder (that has to be named in your PATH, however, to find the java and javac tools).
You only need a CLASSPATH when you have other code which is a dependency for your application. If you are writing a database, you will need JDBC files, and if you use MigLayout, you have to put the Mig files somewhere. The CLASSPATH tells the javac tool and the JVM where those classes are. You should not set a permanent CLASSPATH, in which case the compiler and JVM will search from the current directory; that is its default behaviour.
If you have a download you need to use, you can tell the JVM where to find it by providing a CLASSPATH like C:\...\MigLayoutFolder\Mig.jar;.; but only for the current app. Your next app will probably require a different CLASSPATH.
 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not set a permanent CLASSPATH, in which case the compiler and
JVM will search from the current directory; that is its default behavior.

In the following code test3 and A both are present in pack1 directory and if i compile
test3.java it should search for class A in the current directory as its the default behavior
but it doesn't. and compilation error is the result.




Also
If you have a download you need to use, you can tell the JVM where to find it by providing
a CLASSPATH like C:\...\MigLayoutFolder\Mig.jar;.; but only for the current app. Your next app will probably
require a different CLASSPATH.


C:\...\MigLayoutFolder\Mig.jar;.; means search in C:\...\MigLayoutFolder\Mig.jar and if not found search in
current directory (.). But searching in current directory is default behavior then why we need to specify
again (.) in classpath...


thanks in advance
with regards
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sawan Mishra wrote:
In the following code test3 and A both are present in pack1 directory and if i compile
test3.java it should search for class A in the current directory as its the default behavior
but it doesn't. and compilation error is the result.


If you don't specify a classpath (either via command line or environment variable), then the current directory is the classpath. However, that may not mean the it will search the current directory. The classpath is used to specify the root -- so the class files that it searches for are in subdirectories from the root, named by their package names.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sawan Mishra wrote:
Also
If you have a download you need to use, you can tell the JVM where to find it by providing
a CLASSPATH like C:\...\MigLayoutFolder\Mig.jar;.; but only for the current app. Your next app will probably
require a different CLASSPATH.


C:\...\MigLayoutFolder\Mig.jar;.; means search in C:\...\MigLayoutFolder\Mig.jar and if not found search in
current directory (.). But searching in current directory is default behavior then why we need to specify
again (.) in classpath...


Specifying a classpath (assuming that there wasn't one previously) replaces the default classpath. Likewise, specifying a classpath in the command line replaces the environment variable classpath (assuming one was set). In other words, there is only one classpath -- setting one doesn't add new directories, it replaces the previous settings.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sawan Mishra wrote:classpath is the "class search path".Thus java
and javac commands search for sourcefile in the
classpath mentioned.

No. The classpath is not what java and javac use to find source files - only for finding compiled class files.

Sawan Mishra wrote:But even if I haven't mentioned .(to search
in current directory) as one of the
element of classpath then also javac and java
commands succesfully compile and run any class
file present in the current directory...why??

Because if you have no classpath set at all, java and javac use the current directory "." as the default classpath.

Sawan Mishra wrote:
what I think is compiler should search for
source file in C:\Program Files\Java\jdk1.5.0_06\bin
directory and it shouldn't be able to find my
source file unless i use . as one of the classpath
elemnt i.e,C:\Program Files\Java\jdk1.5.0_06\bin;.;

The bin directory of your JDK does not belong in the classpath. There are no class files in the bin directory of your JDK. You should never save your own files in the bin directory of the JDK.

Do not put the bin directory of the jdk in the classpath. You should add it to the PATH (that is not the same as the CLASSPATH).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!