V
V
Vitaly Vitrenko2015-07-24 18:11:24
Java
Vitaly Vitrenko, 2015-07-24 18:11:24

Why is the CLASSPATH variable ignored after creating a package in a Java program?

The CLASSPATH environment variable contains paths to several .jar libraries (.;D:\lib1.jar; D:\lib2.jar). If you do not use packages (more precisely, use the default package) in programs, access to classes from these libraries is carried out without problems. But if you use a package, javac throws an error saying it doesn't know such classes.
That is, for example, this compiles fine:

javac C1.java

public class C1 {
    public void method() {
        ClassFromLib1.method();
    }
}

And this is in error.
javac C1.java

package P1; 
public class C1 {
    public void method() {
        ClassFromLib1.method();
    }
}


error: cannot find symbol
ClassFromLib1.method();
symbol: variable ClassFromLib1

Why is this happening?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy Vitrenko, 2015-07-24
@Vestail

Problem solved. It turns out that the classes from the libraries that I used were in the default package, and in Java you cannot call classes from the default package in any named package.

P
protven, 2015-07-24
@protven

Because the C1 class must be in the P1 directory and it must be compiled . In
general, the hierarchy of packages and directories in which the classes are located must match.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question