What's the default classpath when not specifying classpath?
Asked Answered
O

4

45

Wondering what will be the default classpath when not specifying classpath option?

Ovary answered 22/11, 2011 at 13:34 Comment(5)
$JRE_HOME and the current working directory, as far as I recall.Makassar
Create a class that just prints out System.getProperty("java.class.path") and you'll know :-)Nap
@StijnGeukens, Don't forget that we need to run that code form the command line, otherwise the IDE will modify the -classpath flag.Punic
@Pacerier: indeed, valid point.Nap
Please note that "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.)". That is, the majority of classes loaded at runtime is not specified in the classpath.Avocation
H
45

The current working directory (.).

From The Java™ tutorials: PATH and CLASSPATH:

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.

Does this include subdirectories?

No, no entry in the classpath is "recursive". You have to list each subdirectory (or jar) explicitly. However, if you have an Example.class file representing class pkg.subpkg.Example, and the default classpath is used, then this file should live in ./pkg/subpkg/Example.class.

If java attempts to resolve pkg.subpkg.Example it will look in /pkg/subpkg of each classpath entry. I.e. you do not have to list ., pkg, and pkg/subpkg in the classpath, only ..

Huggermugger answered 22/11, 2011 at 13:36 Comment(2)
What does this even mean? Current directory for what? Where the java.exe file is? Where the source is? Very confused....Spearwort
The current working directory is the directory you last cd'ed into. If your Windows shell says C:\dir\subdir> then the current working directory is C:\dir\subdir (or on *nix, if your prompt says something like user@machine:/usr/bin$ then the current working directory is /usr/bin).Huggermugger
N
26

I think people are answering this person's question too literally. Yes, CLASSPATH defaults to ".", but there are a bunch of classes that automatically get loaded even when you don't set CLASSPATH or use the -classpath command line argument.

The following is a good place to learn about this process:

http://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html

Nympho answered 11/7, 2014 at 18:56 Comment(2)
the CLASSPATH the question refers to is that used by the classloder loading user and third party components. Other two classloaders exist: - external library classloader - java standard libraries classloader. These classloaders know by themself where to look for classes.Bluegrass
This is the answer I was looking for. Thank you very much for the link. I was very confused about how the JRE libraries got loaded, since their location wasn't on the class path.Speechless
I
9

It is the current Working directory "."

You can check it out your self too

class CheckClassPath{
    public static void main(String args[]){
        System.out.println(System.getProperty("java.class.path"));
    }
}
Incisive answered 22/11, 2011 at 13:48 Comment(0)
O
5

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings. official manual

Orangeade answered 22/11, 2011 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.