GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Unable to compile a java source file from command line. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to compile a java source file from command line." Watch "Unable to compile a java source file from command line." New topic
Author

Unable to compile a java source file from command line.

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
Hi,

My directory structure is as follows.

C:\Home_Dir>

Inside this directory I have a subdirectory Desktop and inside Desktop I have source files A.java and B.java.

A.java & B.java look as follows respectively.





I compiled these files using the following command.

C:\Home_Dir>javac -d Desktop Desktop\A.java Desktop\B.java

And the following files got created.
Desktop-->com-->test-->ClassFiles-->A.class
Desktop-->com-->test-->ClassFiles-->B.class

Note - I didn't have com subfolder already created. javac did it for me.

Now I have a third class C.java in desktop folder that needs to access A.class and B.class and this part is not working.

Class C.java looks as follows.


When I try to compile C.java using the following command..

C:\Home_Dir>javac -cp Desktop Desktop\C.java

I get the following error.

Desktop\C.java:7: error: cannot access A
A.aStaticMethod();
^
bad source file: Desktop\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the sou
rcepath.
1 error


Could somebody please advice.
Thanks for your help.

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2452
    
    8

Assuming your C.java is in the "default" package.

Why don't you just do "javac Desktop\C.java"? The classes A and B will also compile because class C uses them.

By the way, you typed main wrong.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
Hi Tsang,

I tried it but it didn't work cause it couldn't find A.class and B.class.

C:\Home_Dir>javac Desktop\C.java gave me following errors.

Desktop\C.java:1: error: package com.test.ClassFiles does not exist
import com.test.ClassFiles.*;
^
Desktop\C.java:7: error: cannot find symbol
A.aStaticMethod();
^
symbol: variable A
location: class C
Desktop\C.java:8: error: cannot find symbol
B.bStaticMethod(); }
^
symbol: variable B
location: class C
Desktop\C.java:15: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class C
Desktop\C.java:15: error: cannot find symbol
A a = new A();
^
symbol: class A
location: class C
Desktop\C.java:16: error: cannot find symbol
B b = new B();
^
symbol: class B
location: class C
Desktop\C.java:16: error: cannot find symbol
B b = new B();
^
symbol: class B
location: class C
7 errors

I was not aware that compiling C.java would compile A.java and B.java too. Does that really happen? Does javac look for the class files or the source files. Now I'm really confused.

What do you mean by 'C.java is in the "default" package'? I don't have a package statement in C.java file. Would that mean it's in the default package?

I was thinking I should mention class path (-cp Desktop) so javac could find my com folder that contains test subfolder and then ClassFiles and then find A.class and B.class.
Should it be the .java files?

I just corrected the main method. Thanks
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2452
    
    8

Chan Ag wrote:I was not aware that compiling C.java would compile A.java and B.java too. Does that really happen? Does javac look for the class files or the source files. Now I'm really confused.
What do you mean by 'C.java is in the "default" package'? I don't have a package statement in C.java file. Would that mean it's in the default package?
I was thinking I should mention class path (-cp Desktop) so javac could find my com folder that contains test subfolder and then ClassFiles and then find A.class and B.class.
Should it be the .java files?


Yes compiling C.java will also compile A and B java files. By "default" package, this means the java source file is NOT in any folders. Your files A and B are in some package hence inside the com\test\ClassFiles folder.

The classpath (-cp) for javac is irrelevant in your case. Classpath is not the same as folder. Classpath is usually used for pointing to some external library files your code needs that is outside from the standard Java API.

Your file structure should look like:



From this the com folder and C.java file are inside the Desktop folder. Inside com folder is the test folder. Inside test folder is the ClassFiles folder. Inside ClassFiles folder is the A.java and B.java files.
So if you are in the Home_Dir folder and type "javac Desktop\C.java", the C.class file gets created inside Desktop folder, the A.class and B.class files inside com\test\ClassFiles folder.


Are you using a IDE? The file structure will get more clear if you use one.

While I'm at it, for javac you may want to also use the -d flag for specifying a different location for the compiled class files.

You are getting compiler errors because I think your file structure is wrong.

Hope this helps.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

I think you're working in Desktop folder. Then you might use CD command to go inside it..

C:\Home_Dir> CD Desktop

I think you have placed source files of class A and B in that folder. If that is true type following codes in command line..

C:\Home_Dir\Desktop> javac -d . A.java
C:\Home_Dir\Desktop> javac -d . B.java

You have saved class C in desktop folder too.(You have typed main method wrong. Therefore you will get a runtime error. Therefore type it correctly..!)

C:\Home_Dir\Desktop> javac C.java

But, if you have any class file named 'A.class' or 'B.java' in Desktop folder, you will get compile error..!
If class C finished compile..,

C:\Home_Dir\Desktop> java C

You will have success..


Ramesh-X
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
Hi Tsang,

I just tested a few things.. and I'm surprized.

1. If I delete A.java and B.java from Desktop and if com.test.ClassFiles (in Desktop) has A.class and B.classactually works. It compiles my C.java file. I'm able to execute it by typing


So it seems like javac got confused when I had A.java and B.java in desktop. Or so it seems. I'm sort of confused still.

2. If I delete the com folder completely ( it'd delete the class files too) and C.class from desktop and have desktop contain A,B and C source files, and I give following command,



all three files compiled. I got the folder com created ( with the subfolders containing the A and B class files) and I got a C.class in my desktop.
This seems funny cause I thought if you'd first compile A.java and B.java specifying destination as Desktop and then separately compile C.java, it should mean the same thing.
I don't understand this behavior though.

And the most weird thing is while works, doesn't work if I have com folder and the class files and if I don't have com folder and class files and only have A and B source files.

Could somebody please explain this behavior. It seems little strange to me..

Thanks so much ..

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
Hi Tsang,

I read your response just now.

Yea, my folder structure is the same as what you've mentioned. Just I also had A.java and B.java in the Desktop folder. And no, I wasn't using an IDE specifically to test the imports and package subtelities ( for SCJP ).

Deleteing A.java and B.java from Desktop and then trying to compile C.java surprizingly worked. No idea, how... A.class and A.java are two different files right? And A.class was in the com.test.Classfiles folders anyway.. Pretty weird..
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2452
    
    8

Chan Ag wrote:Hi Tsang,

I read your response just now.

Yea, my folder structure is the same as what you've mentioned. Just I also had A.java and B.java in the Desktop folder. And no, I wasn't using an IDE specifically to test the imports and package subtelities ( for SCJP ).

Deleteing A.java and B.java from Desktop and then trying to compile C.java surprizingly worked. No idea, how... A.class and A.java are two different files right? And A.class was in the com.test.Classfiles folders anyway.. Pretty weird..


Ah about the -d flag for javac, it changes folder.

So if you are in Home_dir and type "javac -d Desktop ..." it will cd to desktop compile the java files there.

About the A and B java files... If you have them in the same folder (aka java package), then you don't need to use import statements.

Did you say SCJP? You need to know this is back of your head
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
Are you trying to say I talk out of the back of my head.

Well you know what. I've kind of been polite though I know you wouldn't see that. But never mind..

Very humbly - If you think I said something really stupid, I'd say .. I'm still learning... Though suit yourself.. Opinions are a personal thing anyway.

And if you meant something else - my apologies.

And to others - the topic is still open.

Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I'm pretty sure that K.Tsang meant "You need to know this like the back of your hand" (which is an English idiom for "You need to know this thoroughly and without hesitation" and easy to get wrong for non-English speakers).
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2302
    
  49
Chan Ag wrote:Are you trying to say I talk out of the back of my head.

I think what K Tsang was trying to say was for SCJP you need to know how to do this without having to think about it - I don't think he/she meant it as an insult, I suspect it is just a saying in whichever country he/she is from.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
I'm sorry then. I misunderstood. I ain't good with idioms and I kinda suck at expressing and comprehending not too easy stuff. I know you were trying to help but .. just I misunderstood things..

Don't mind. Would you?
And moderators, bartenders and the other experts please don't ban me. and thanks.


Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2302
    
  49
Chan Ag wrote:I'm sorry then. I misunderstood. I ain't good with idioms and I kinda suck at expressing and comprehending not too easy stuff. I know you were trying to help but .. just I misunderstood things..

No harm done. It's very easy to misunderstand posts especially when posters/readers aren't using their native language. I guess this just shows that in this sort of situation it's best to ask the poster to clarify what they mean rather than jumping to conclusions.
 
GeeCON Prague 2014
 
subject: Unable to compile a java source file from command line.