aspose file tools*
The moose likes Beginning Java and the fly likes compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compilation error" Watch "compilation error" New topic
Author

compilation error

arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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




Thanks-Arun
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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

Joined: Oct 13, 2005
Posts: 39393
    
  28
Is that protected field “visible” in package xyz? Probably not, which the compiler will have noticed.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tony Fu
Greenhorn

Joined: Sep 04, 2008
Posts: 10
Because the Example class and A are not in the same package


Do everything by yourself!
arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29

Here is my attachment..


[Download error Snapshot.bmp] Download

arunseege paramashivappa
Greenhorn

Joined: Nov 02, 2010
Posts: 29


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
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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();


[Download CompilationError2.bmp] Download

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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
Greenhorn

Joined: Nov 02, 2010
Posts: 29

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

Joined: Sep 04, 2008
Posts: 10
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

Joined: Sep 04, 2008
Posts: 10
You should type "javac -d . *.java",so it can make package
Tony Fu
Greenhorn

Joined: Sep 04, 2008
Posts: 10
the attachement is my result


[1111.jpg]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compilation error