Problem compiling java programs

L

lonelyplanet999

While I was writing java programs with abstract class, I met below
compilation problems. Hope someone can help.

Case 1
======

File: c:\javapgm\aclass2\aclass1\Aclass1.java

package aclass1;
public abstract class Aclass1 {
private double price;
private String model;
public abstract void goFast();
public abstract void goUpHill();
}

Aclass1.java compiled successfully.

File: c:\javapgm\aclass2\Aclass2.java

import aclass1.Aclass1;

public abstract class Aclass2 extends Aclass1 {
public abstract void goDownHill();
public void goSlow() {
System.out.println ("This is non-abstract method goSlow()");
}
}

Aclass2.java compiled successfully.

File: c:\javapgm\Aclass3.java

import aclass2.Aclass2;

public class Aclass3 extends Aclass2 {
public void goFast() {
System.out.println ("This is non-abstract method goFast()");
}
public void goUpHill() {
System.out.println ("This is non-abstract method goUpHill()");
}
public void goDownHill() {
System.out.println ("This is non-abstract method goDownHill()");
}
}

Compiling Aclass3.java returned below error.

Aclass3.java:1: Cannot access aclass2.Aclass2
bad class file: .\aclass2\Aclass2.class
class file contains wrong class: Aclass2
Please remove or make sure it appears in the correct subdirectory of
the classpath.
import aclass2.Aclass2
^
1 error

Similar problem happened for below class definitions

Case 2
======

File: c:\javapgm\aclass2\aclass1\Aclass1.java

package aclass1;
public abstract class Aclass1 {
private double price;
private String model;
public abstract void goFast();
public abstract void goUpHill();
}

Aclass1.java compiled successfully.

File: c:\javapgm\aclass2\Aclass4.java

import aclass1.Aclass1;

public class Aclass4 extends Aclass1 {
public void goFast() {
System.out.println ("This is non-abstract method goFast()");
}
public void goUpHill() {
System.out.println ("This is non-abstract method goUpHill()");
}
public void goSlow() {
System.out.println ("This is non-abstract method goSlow()");
}
}


Aclass4.java compiled successfully.

File: c:\javapgm\Aclass5.java

import aclass2.Aclass4;

public class Aclass5 extends Aclass4 {
// public void doMore () {
// System.out.println("Calling Aclass4.goFast() inside
Aclass5.doMore()");
// goFast();
// }
public staic void main (String[] args) {
//doMore();
}
}

Compiling Aclass5.java returned below error

Aclass5.java:10: <identifier> expected
public static void main (String[] args) {
^
Aclass5.java:12: ';' expected
}
^
Aclass5.java:3: cannot access aclass2.Aclass4
bad class file: .\aclass2\Aclass4.class
class file contains wrong class: Aclass4
Please remove or make sure it appears in the correct subdirectory of
the classpath.
import aclass2.Aclass4;
^
3 errors
 
P

Peter Kirk

lonelyplanet999 said:
File: c:\javapgm\aclass2\Aclass2.java

import aclass1.Aclass1;

public abstract class Aclass2 extends Aclass1 {
public abstract void goDownHill();
public void goSlow() {
System.out.println ("This is non-abstract method goSlow()");
}
}

Is this class in a package, eg
package aclass2;

Peter
 
L

lonelyplanet999

Peter Kirk said:
Is this class in a package, eg
package aclass2;

Yes, I missed the package statement. I modified the packages as below:

c:\javapgm\aclass2\aclass1\Aclass1.java

package aclass1;
public abstract class Aclass1 {
private double price;
private String model;
public abstract void goFast();
public abstract void goUpHill();
}

c:\javapgm\aclass2\Aclass2.java

package aclass2;
import aclass1.Aclass1;

public abstract class Aclass2 extends Aclass1 {
public abstract void goDownHill();
public void goSlow() {
System.out.println ("This is non-abstract method goSlow()");
}
}

c:\javapgm\Aclass3.java

package aclass3;
import aclass2.Aclass2;
import aclass2.aclass1.Aclass1;

public class Aclass3 extends Aclass2 {
public void goFast() {
System.out.println ("This is non-abstract method goFast()");
}
public void goUpHill() {
System.out.println ("This is non-abstract method goUpHill()");
}
public void goDownHill() {
System.out.println ("This is non-abstract method goDownHill()");
}
}

Compilation of Aclass1, & Aclass2 packages succeeded but Aclass3
failed with below error.

Aclass3.java:3: cannot access aclass2.aclass1.Aclass1
bad class file: .\aclass2\aclass1\Aclass1.class
class file contains wrong class: aclass1.Aclass1
Please remove or make sure it appears in the correct subdirectory of
the classpath.
import aclass2.aclass1.Aclass1;
^
1 error

If 'import aclass2.aclass1.Aclass1;' statement in Aclass3.java
commented out, below error reported.

Aclass3.java:5: cannot access aclass1.Aclass1
file aclass1\Aclass1.class not found
public class Aclass3 extends Aclass2 {
^
1 error
 
P

Peter Kirk

lonelyplanet999 said:
Yes, I missed the package statement. I modified the packages as below:

c:\javapgm\aclass2\aclass1\Aclass1.java

<snip>

Hmm. I think you seem to be swapping things around a bit each time. You have
to make sure that the java classes are in the same directory as the pacakage
statement indicates.

Eg. if I have a directory/file like this (where the source files are stored
in a tree under the "src" directory):

c:\myproject\src\a\b\c\MyClass.java

then the package statement in MyClass.java should look like this:

package a.b.c;

Likewise, if I have this file:

c:\myproject\src\d\e\f\MyOtherClass.java

then the package statement in MyOtherClass.java should look like this:

package d.e.f;

Then, if MyOtherClass should extend MyClass (or just use it in some way),
then I need an import statement in MyOtherClass, like this:

import a.b.c.MyClass;


Hope you can make some sense of that, and get your classes to compile.

Peter
 
L

lonelyplanet999

Peter Kirk said:
<snip>

Hmm. I think you seem to be swapping things around a bit each time. You have
to make sure that the java classes are in the same directory as the pacakage
statement indicates.

Eg. if I have a directory/file like this (where the source files are stored
in a tree under the "src" directory):

c:\myproject\src\a\b\c\MyClass.java

If I don't interpret wrongly, I also follow the rule.

The only difference is the classes defined recursively extends another
one, as described below:

c:\myproject\src\Aclass3.java has only one non-abstract class named
Aclass3. This class extends another class Aclass2.

c:\myproject\src\aclass2\Aclass2.java has only one abstract class
named Aclass2. This class extends another class Aclass1.

c:\myproject\src\aclass2\aclass1\Aclass1.java has only one abstract
class named Aclass1. This class doesn't extend any other class.

The first statement inside Aclass1.java is
package aclass1;

The first 2 statements inside Aclass2.java are
package aclass2;
import aclass1.Aclass1;

The first 3 statements inside Aclass3.java are
package aclass3;
import aclass2.Aclass2;
import aclass2.aclass1.Aclass1;

Compilation of Aclass1.java & Aclass2.java succeeded but that of
Aclass3.java failed. Even if I commented statement 'import
aclass2.aclass1.Aclass1;', compilation still failed.

What I don't understand is what java rule I have violated that caused
the compilation failure.
 
P

Peter Kirk

c:\myproject\src\aclass2\aclass1\Aclass1.java has only one abstract
class named Aclass1. This class doesn't extend any other class.

The first statement inside Aclass1.java is
package aclass1;

But isn't this wrong? You have Aclass1.java in directory \aclass2\aclass1\
yet the package statement says just aclass1.

The first 2 statements inside Aclass2.java are
package aclass2;
import aclass1.Aclass1;

Here you are saying that Aclass1 is in package aclass1....
The first 3 statements inside Aclass3.java are
package aclass3;
import aclass2.Aclass2;
import aclass2.aclass1.Aclass1;

.....and here you are saying that Aclass1 is in package aclass2.aclass1

Compilation of Aclass1.java & Aclass2.java succeeded but that of
Aclass3.java failed. Even if I commented statement 'import
aclass2.aclass1.Aclass1;', compilation still failed.

I'm surprised that any of them compiled. As far as I can see Aclass1 should
not have compiled because its package statement does not match the directory
in which it lies. This would mean that Aclass2 cannot compile - because it
tries to use the non-existent Aclass1. Likewise for Aclass3.
 
L

lonelyplanet999

Peter Kirk said:
But isn't this wrong? You have Aclass1.java in directory \aclass2\aclass1\
yet the package statement says just aclass1.



Here you are saying that Aclass1 is in package aclass1....


....and here you are saying that Aclass1 is in package aclass2.aclass1



I'm surprised that any of them compiled. As far as I can see Aclass1 should

Last time I 'cd c:\myproject\src\aclass2\aclass1\' then run
c:\j2sdk1.4.1_01\bin\javac Aclass1.java. The compilation really
returned no error.

Then I 'cd c:\myproject\src\aclass2\', then run
c:\j2sdk1.4.1_01\bin\javac Aclass2.java. The compilation really
returned no error, too.

Compilation failed only when I run javac Aclass3.java after I 'cd
c:\myproject\src\'
not have compiled because its package statement does not match the directory
in which it lies. This would mean that Aclass2 cannot compile - because it
tries to use the non-existent Aclass1. Likewise for Aclass3.

As you suggested, now I changed the package declaration statements as
below:

In c:\myproject\src\aclass2\aclass1\Aclass1.java I wrote
package aclass2.aclass1;

In c:\myproject\src\aclass2\Aclass2.java I wrote
package aclass2;
import aclass2.aclass1.Aclass1;

In c:\myproject\src\Aclass3.java I wrote
package aclass3;
import aclass2.Aclass2;
import aclass2.aclass1.Aclass1;

Then I called javac at directory c:\myproject\src for all the 3 .java
files i.e.

\j2sdk1.4.1_01\bin\javac aclass2\aclass1\Aclass1.java
\j2sdk1.4.1_01\bin\javac aclass2\Aclass2.java
\j2sdk1.4.1_01\bin\javac Aclass3.java

All compilation succeeded then. Also, all Aclass?.java corresponding
..class files were put into their respective directories along with
their associated .java filed.

I would like to ask if Aclass3.java is not under the same directory
root as Aclass2.java & Aclass1.java, say Aclass3.java under
c:\myproject\, Aclass2.java & Aclass1.java under c:\classes\ &
c:\classes\aclass1, will java allows that ?
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top