Tim
Tim

Reputation: 99418

Is the current path `.` in the classpath by default?

Do the second command add the current path as another path for searching?

javac -cp /home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

javac -cp .:/home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

Is it the correct way to separate multiple paths, by a colon?

Isn't the current path always in ClassPath by default, and thus no need to explicitly specify?

Thanks.

Upvotes: 13

Views: 21556

Answers (5)

Dharmendrasinh Chudasama
Dharmendrasinh Chudasama

Reputation: 1117

Solution: Just delete environment variable "CLASSPATH", and then restart your terminal (command prompt)

  • Explaining: Once you will remove then it will use it's (CLASSPATH) default value, for more information you can take reference from here

Upvotes: 0

Voicu
Voicu

Reputation: 17850

From Oracle's page on setting the class path:

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.

Use ; for Windows and : for Unix-like operating systems as a separator for multiple paths.

Upvotes: 17

Amr Lotfy
Amr Lotfy

Reputation: 2997

If you add classpath then current path is omitted, which is a very very unpleasant and unexpected behavior :(

Moreover to add current path I found (at least for ubuntu) that IT IS NOT ENOUGH to add . in classpath but you have to add ./*

For example (this will not work)

java -ea -cp ".:lib/*" org.testng.TestNG suites/regression.xml

will NOT work if you have a jar file in current path

the correct one is

java -ea -cp "./*:lib/*" org.testng.TestNG suites/regression.xml

I hope no one shoots himself or have a heart attack!

Upvotes: 10

rolfl
rolfl

Reputation: 17707

From the help page (FOR WINDOWS):

-classpath classpath
-cp classpath

    Specifies a list of directories, JAR files, and ZIP archives to
    search for class files. Separate class path entries with semicolons
    (;). Specifying -classpath or -cp overrides any setting of the
    CLASSPATH environment variable.

    If -classpath and -cp are not used and CLASSPATH is not set, then the
    user class path consists of the current directory (.).

Note that, on windows, the path separator is a ; semicolon.

On other platforms the separator is the colon :.

This conforms with the standard path-like systems on the various platforms.

Upvotes: 5

Jigar Joshi
Jigar Joshi

Reputation: 240890

Do the second command add the current path as another path for searching?

Yes

Is it the correct way to separate multiple paths, by a colon?

depends on platform, in unix platform : works, in windows you need ;

Isn't the current path always in ClassPath by default, and thus no need to explicitly specify?

Current directory is present by default unless you override it with -cp in first case it is not present in second case it is

Upvotes: 4

Related Questions