Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem compiling java programs

Reply
Thread Tools

Problem compiling java programs

 
 
lonelyplanet999
Guest
Posts: n/a
 
      10-16-2003
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
 
Reply With Quote
 
 
 
 
Peter Kirk
Guest
Posts: n/a
 
      10-16-2003
"lonelyplanet999" <> wrote in message
news: om...
> 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



 
Reply With Quote
 
 
 
 
lonelyplanet999
Guest
Posts: n/a
 
      10-16-2003
"Peter Kirk" <peter_kirk@PLEASE_DONT_SPAM_ME_ANYMORE_alpha-gruppen.NO_MORE_dk_ARGGGGH> wrote in message news:<3f8e474c$0$54856$. dk>...
> "lonelyplanet999" <> wrote in message
> news: om...
> > 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;


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

>
> Peter

 
Reply With Quote
 
Peter Kirk
Guest
Posts: n/a
 
      10-17-2003
"lonelyplanet999" <> wrote in message
news: om...
> 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




 
Reply With Quote
 
lonelyplanet999
Guest
Posts: n/a
 
      10-17-2003
"Peter Kirk" <peter_kirk@PLEASE_DONT_SPAM_ME_ANYMORE_alpha-gruppen.NO_MORE_dk_ARGGGGH> wrote in message news:<3f8f9b23$0$54835$. dk>...
> "lonelyplanet999" <> wrote in message
> news: om...
> > 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


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.

>
> 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

 
Reply With Quote
 
Peter Kirk
Guest
Posts: n/a
 
      10-19-2003
"lonelyplanet999" <> wrote in message
news: om...

<snip>

> 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.




 
Reply With Quote
 
lonelyplanet999
Guest
Posts: n/a
 
      10-20-2003
"Peter Kirk" <peter_kirk@PLEASE_DONT_SPAM_ME_ANYMORE_alpha-gruppen.NO_MORE_dk_ARGGGGH> wrote in message news:<3f92a23f$0$54862$. dk>...
> "lonelyplanet999" <> wrote in message
> news: om...
>
> <snip>
>
> > 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


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 ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 09:47 PM
Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 08:55 PM
Compiling JAVA programs using .jar libs Piotrek Java 2 06-24-2008 09:54 PM
Compiling when libedit is in path Is there a trick to compiling Ruby when libedit must exist in the search path? Can you statically link to readline 5.0 in some manner? -- Lon Baker Lon Baker Ruby 1 03-21-2005 08:57 AM
PROBLEM: compiling C programs that link to Python Nikola Milutinovic Python 0 01-04-2004 04:10 PM



Advertisments