File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath" Watch "classpath" New topic
Author

classpath

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Hi
I read that java command looks classpath for required classes.Now suppose i don't set classpath at environment variable neither through -classpath option although i put jar file of my class in jre/lib/ext folder.Will it run successfully from anywhere without mentioning classpath ?

Thanks.


OCA7
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

ankita modi. wrote:Hi
I read that java command looks classpath for required classes.Now suppose i don't set classpath at environment variable neither through -classpath option although i put jar file of my class in jre/lib/ext folder.Will it run successfully from anywhere without mentioning classpath ?
Thanks.

Here's a line regarding compiler from Javac Doc: -

-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

R. Jain wrote:
ankita modi. wrote:Hi
I read that java command looks classpath for required classes.Now suppose i don't set classpath at environment variable neither through -classpath option although i put jar file of my class in jre/lib/ext folder.Will it run successfully from anywhere without mentioning classpath ?
Thanks.

Here's a line regarding compiler from Javac Doc: -

-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.


Your link is dead badly.I believe that as i put jar in jre/lib/ext folder so no need to mention classpath at -cp or environment variable, as javac and java command first search classes at jre/lib/rt.jar then jre/lib/ext folder then at environment variable classpath if any and then finally -cp classpath if any and yes -cp does overwrite environment variable classpath.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
ankita modi. wrote: . . . Your link is dead badly.I believe that as i put jar in jre/lib/ext folder so no need to mention classpath at -cp or environment variable, as javac and java command first search classes at jre/lib/rt.jar then jre/lib/ext folder then at environment variable classpath if any and then finally -cp classpath if any and yes -cp does overwrite environment variable classpath.
The bits of that which I can actually understand look like poor advice.
Don’t put any of your work into the Java installation folders.
Use the -cp option, unless you can arrange everything to be in the same directory structure.
No -cp does not overwrite anything, but overrides the system classpath.
It is a bad idea to set a system classpath at all, even though older books tell you how to do it.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

my memory is poor so please check but i think java extension mechanism( i.e. putting files in jre/lib/ext so that java/javac automatically loads class files from there) work ONLY FOR CLASS FILES AND NOT FOR JAR FILES.
if you place .class file there it will pick but not jar file. again i maybe wrong so please make a sample program and see for yourself.

And Campbell is right. it is not a good idea to set classpath like this. the best way is to by specifying classpath using -cp switch. putting class files there in jre/lib/ext folder can cause conflicts if you have 2 version of classfiles in your classpathh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Worse things can happen than conflict of types if you put your own .class files in the Java installation folder. What happens when you uninstall it and install a new version of Java?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Actually I think, but I am not certain, that the javac tool and the JVM will use the first file found which matches the name required and then stop looking. You would not then get a type conflict, but you might select a file different from what you expect, leading to errors difficult to diagnose.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

gurpeet singh wrote:my memory is poor so please check but i think java extension mechanism( i.e. putting files in jre/lib/ext so that java/javac automatically loads class files from there) work ONLY FOR CLASS FILES AND NOT FOR JAR FILES.
if you place .class file there it will pick but not jar file. again i maybe wrong so please make a sample program and see for yourself.

And Campbell is right. it is not a good idea to set classpath like this. the best way is to by specifying classpath using -cp switch. putting class files there in jre/lib/ext folder can cause conflicts if you have 2 version of classfiles in your classpathh


Jar files will surely work there, i don't understand any reason to not work.And there will be no conflict, if required class is found at first classpath then compiler wont look next classpath if any.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
According to this link, yes jar files placed at ext folder is added in the classpath. Check the example "Running AreaApp by Using the Extension Mechanism" in the link.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Stilll don’t like it. All is well until you reinstall Java and the folder is deleted!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

ankita modi. wrote:
gurpeet singh wrote:my memory is poor so please check but i think java extension mechanism( i.e. putting files in jre/lib/ext so that java/javac automatically loads class files from there) work ONLY FOR CLASS FILES AND NOT FOR JAR FILES.
if you place .class file there it will pick but not jar file. again i maybe wrong so please make a sample program and see for yourself.

And Campbell is right. it is not a good idea to set classpath like this. the best way is to by specifying classpath using -cp switch. putting class files there in jre/lib/ext folder can cause conflicts if you have 2 version of classfiles in your classpathh


Jar files will surely work there,


It doesn't matter if it works. It's the wrong place to put your jar files. That's not what that directory is for.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

then what is the real use of jre/lib/ext folder ??


OCPJP 6 86%
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Nikhil Sagar wrote:then what is the real use of jre/lib/ext folder ??


There are a few speical jar files that have to go there to work correctly. The only case I recall off the top of my head is certain security/encryption stuff, though that may have been back before the encryption classes were part of the core API. It's for stuff that's supposed to get loaded by the "null" or "default" classloader, and I believe the JVM treats it with a higher level of trust than classes loaded with other classloaders. Putting something there that doesn't belong there is a security risk. Plus it just leads to a mess if you have more than a couple of apps that you're running. Better to give each app its own directory tree of jar files and resources.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Jeff Verdegan wrote:
Nikhil Sagar wrote:then what is the real use of jre/lib/ext folder ??


There are a few speical jar files that have to go there to work correctly. The only case I recall off the top of my head is certain security/encryption stuff, though that may have been back before the encryption classes were part of the core API. It's for stuff that's supposed to get loaded by the "null" or "default" classloader, and I believe the JVM treats it with a higher level of trust than classes loaded with other classloaders. Putting something there that doesn't belong there is a security risk. Plus it just leads to a mess if you have more than a couple of apps that you're running. Better to give each app its own directory tree of jar files and resources.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath