Reputation: 4309
My directory structure is very simple: in one directory, I have one file called Foo.java and one file called Bar.java
Foo.java:
package Foo;
public class Foo {
}
Bar.java:
import Foo.*;
public class Bar {
public static void main(String argv[]) {
Foo foo; // This line breaks compilation
System.out.println("Hello world!");
}
}
Everything compiles fine without the line Foo foo
. But when I add it, I get:
$ javac Bar.java Foo.java
Bar.java:5: error: cannot access Foo
Foo foo;
^
bad class file: ./Foo.class
class file contains wrong class: Foo.Foo
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
What am I doing wrong?
Upvotes: 1
Views: 102
Reputation: 1095
If both Foo.java
and Bar.java
are in the same Foo
package, you don't need to import Foo.*
package (this way import static methods and shared constants of Foo class); You just write:
package Foo;
public class Bar {
public static void main(String[] args) {
Foo foo;
System.out.println("Hello World");
}
}
If you have made two different packages: Foo
for Foo.java
and Bar
for Bar.java
, you write:
package Bar;
import Foo.Foo; // or import Foo.*;
public class Bar {
public static void main(String[] args) {
Foo foo;
System.out.println("Hello World");
}
}
Upvotes: 2
Reputation: 8354
javac Bar.java Foo.java
, this is plain wrong .Foo
is in a package named Foo
(please use a different name to avoid confusion).
you'll have to use javac -d "path to classes dir here" Foo/Foo.java
and javac -d "path to classes dir here " Bar.java
.
Upvotes: 1