classpath

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 ?

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.

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.

-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.

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.

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: 44016

33

posted Sep 28, 2012 02:19:08

0

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: 44016

33

posted Sep 28, 2012 02:20:59

0

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.

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.

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.

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.

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.