Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Steven Simpson Newsgroups: comp.lang.java.programmer Subject: Re: Is it necessary the source file should be at the same location with the class file? Date: Sat, 23 Jul 2016 20:10:55 +0100 Organization: A noiseless patient Spider Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: mx02.eternal-september.org; posting-host="d52490782ece4152e133f55aeb388f67"; logging-data="22765"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/xG+n/b66R0y8EeZAyko82UQWShbte4A=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:wzeNrU8lXn3SN5mbSmVacvYS8xI= Xref: csiph.com comp.lang.java.programmer:35112 On 23/07/16 17:55, parmenides wrote: > package compute.shape; > public class Circle{ [snip] > > I compiled the Circle.java and copied the Circle.class to the > compute/shape directory: > > javac Circle.java > cp Circle.class compute/shape > > Then, I compiled the Test.java: > > javac Test.java > > I got a message like this: > > Test.java:5: error: cannot access Circle > Circle c = new Circle(); Did it not also say the following?: bad class file: ./Circle.class class file contains wrong class: compute.shape.Circle Please remove or make sure it appears in the correct subdirectory of the classpath. 1 error > I modified the first line of Test.java as follows: > > import compte.shape.*; > ===> > import compte.shape.Circle; (The typo in the package name won't help.) With the whole-package import, javac first tries to interpret the name Circle as a class in the default package (which the referencing class belongs to). Lo and behold, there is a file called Circle.class in the current directory (the default classpath), so it tries to read the definition from there. However, it declares itself to be the definition of compute.shape.Circle, not Circle, and you get the error above. If you remove Circle.class, javac finds Circle.java, and tries to compile it, and reports a similar error. Keep the source file in the same position in the directory hierarchy as the class file to avoid confusion. They don't have to be in the same directory tree (using -sourcepath, -classpath and -d), just the same relative position within their respective trees. -- ss at comp dot lancs dot ac dot uk