• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Unable to compile a java source file from command line.

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic