Picked the wrong day to quit drinking

A

au714

I think I should be able to compile the following 2 files. Class
A.java specifies interface A. Class B.java in package impl implements
interface A.

A.java is in the current directory (.). B.java is in the directory
impl.


public interface A {
public void a();
}

package impl;
public class B implements A {
public void a() {
System.out.println("B.a()\n");
}
public static void main(String[] args) {
B b = new B();
b.a();
}
}


I run javac from the current directory (.) as follows, and get the
following error:
$> javac A.java impl/B.java
impl/B.java:3: cannot resolve symbol
symbol : class A
location: class impl.B
public class B implements A {
^
1 error


I also tried as:
javac -sourcepath . A.java impl/B.java
This is so simple, what am I missing?


FGB
 
V

Vincent Cantin

I think I should be able to compile the following 2 files. Class
A.java specifies interface A. Class B.java in package impl implements
interface A.

A.java is in the current directory (.). B.java is in the directory
impl.

Here is the mistake, perhaps :

Don't put the class A in the current directory. The current directory is the
"default" package, and you cannot refer to it from outside of it (here, from
the package impl).

I am not sure about this since I didn't test and I never refer a class of
the default package from something outside of the default package, I
remember about having seen this issue somewhere.
 
C

Chris Smith

I think I should be able to compile the following 2 files. Class
A.java specifies interface A. Class B.java in package impl implements
interface A.

A.java is in the current directory (.). B.java is in the directory
impl.

The problem is that the interface A is in the default package, and the
class B is in a package called 'impl'. Classes that are in named
packages may not reference classes from the default package. Move A
into a package (whatever the name) and it will work.

Of course, you would have been using packages in production code anyway,
so this basically qualifies as a problem that just pops up in test code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

au714

Background is that I am writing a code generator that reads 'meta data'
from a database and generates various code components, including Java
source.

There is an optional command line option that says to put the generated
source in a package named 'package.name'. Looks like I need to make
this a mandatory command line option.

Thanks...
 
M

Mike Schilling

Background is that I am writing a code generator that reads 'meta data'
from a database and generates various code components, including Java
source.

There is an optional command line option that says to put the generated
source in a package named 'package.name'. Looks like I need to make
this a mandatory command line option.

Or else define a default package used if nothing was specified.

But Chris is 100% right; avoid using the default package and this sort of
oddity will go away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top