Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation error

 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First i will copy my code here then i will explain the problem i faced...


inside F:\corejava\abc i created A.java as shown below..


inside F:\corejava\xyz i created Example.java as shown below..



when i tried to compile Example.java from the command prompt it is giving me compilation error.
But it worked fine in eclipse editor.

Please correct me where i am wrong.

Thanks-Arun



 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun,

When you're posting code samples, please use code tags and proper indentation so it's easier to read. I've added the code tags for you above. Also when reporting an error, it's a good idea to tell us what the error is. You can even cut-and-paste the error text, or at least a few lines of it.

I'm guessing the problem is that when you compile Example.java, the compiler can't find the A.class that it refers to. You probably will need to give a classpath parameter to the javac command. Something like:

javac -classpath F:\corejava Example.java


It also can be worth learning a build tool like Ant or Maven if you want to do much compiling from a command line.
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that protected field “visible” in package xyz? Probably not, which the compiler will have noticed.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arunseege paramashivappa wrote:
inside F:\corejava\xyz i created Example.java as shown below..



when i tried to compile Example.java from the command prompt it is giving me compilation error.
But it worked fine in eclipse editor.


I am assuming that when you say compilation error -- you meant at line 14, at the line that you commented out? Yes, if that is the location, then it's a compilation error (assuming not commented) -- and with both javac and eclipse.

A subclass is not allowed to access a protected member declared in a super class that is in a different package via a reference that it is not responsible for the implementation.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

In this case, since the "a" reference is not confirmed to be referring to an object that IS-A Example class, it is not allowed.

Henry
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the Example class and A are not in the same package
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my attachment..
Filename: error Snapshot.bmp
Description:
File size: 756 Kbytes
[Download error Snapshot.bmp] Download Attachment
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Campbell, protected variable can be seen and accessed by the subclasses outside the package..

in my above example e.aa is giving me value but a.aa will gives compilation error.Hence i commented the line containing a.aa

Thanks-Arun
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg Charles,

Please find the attachment below

compilation error is shown at import statement at line no.3.
cannot find symbol at line no. 12 i.e, A a=new A();
Filename: CompilationError2.bmp
Description:
File size: 837 Kbytes
[Download CompilationError2.bmp] Download Attachment
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arunseege paramashivappa wrote:Hi Greg Charles,

Please find the attachment below
No, don’t attach .bmp files; copy the text into a new post, please.
 
arunseege paramashivappa
Ranch Hand
Posts: 33
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

F:\corejava>cd abc

F:\corejava\abc>javac *.java

F:\corejava\abc>cd..

F:\corejava>cd xyz

F:\corejava\xyz>javac Example.java
Example.java:3: package abc does not exist
import abc.A;
^
Example.java:5: cannot find symbol
symbol: class A
public class Example extends A {
^
Example.java:12: cannot find symbol
symbol : class A
location: class xyz.Example
A a=new A();
^
Example.java:12: cannot find symbol
symbol : class A
location: class xyz.Example
A a=new A();
^
Example.java:15: cannot find symbol
symbol : variable aa
location: class xyz.Example
System.out.println("aa============"+e.aa);
^
5 errors

F:\corejava\xyz>
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arunseege paramashivappa wrote:Hi Campbell,

F:\corejava>cd abc

F:\corejava\abc>javac *.java

F:\corejava\abc>cd..

F:\corejava>cd xyz

F:\corejava\xyz>javac Example.java
Example.java:3: package abc does not exist
import abc.A;
^
Example.java:5: cannot find symbol
symbol: class A
public class Example extends A {
^
Example.java:12: cannot find symbol
symbol : class A
location: class xyz.Example
A a=new A();
^
Example.java:12: cannot find symbol
symbol : class A
location: class xyz.Example
A a=new A();
^
Example.java:15: cannot find symbol
symbol : variable aa
location: class xyz.Example
System.out.println("aa============"+e.aa);
^
5 errors

F:\corejava\xyz>


you should javac -d ...When you complie,you should take this option -d ,
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should type "javac -d . *.java",so it can make package
 
Tony Fu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the attachement is my result
1111.jpg
[Thumbnail for 1111.jpg]
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use classes in other packages, they are dependencies, and ought to be compiled first. Have a look at this old thread and the Java Tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic