Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javac options and directory structure

 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the past few weeks I have been learning how to manipulate java and javac commands when using a command prompt (on windows). Being able to use the -d command has made cleaning up the directories when compiling so much easier. However one problem that I have run into is inheritance and placing class files in a separate directories. I'll give a visual explanation:

Say I have the directory structure of:



Bob extends Human.
Human and Bob are both under the package structure



I am currently situated inside the root directory, so to compile the human.java file and place it into the classes directory I would use the command:



This places the Human.class in said directory structure. (I know most of you know this but bare with me).
However when I try to compile the Bob.java file with the same command it fails and I get errors stating it cannot find the Human Object/Class. I know that if I compile both files at the same time it would work perfectly but this seems to be quite a long entry into a cmd prompt, and having to recompile over and over with a massive String of text would just become tedious. My two questions are:

Is there a simpler way of doing this?
Do I have to use/can I use multiple javac options when compiling my file?

I have scoured the internet for the past few days to get my head around this but can't quite find the right answer. I am grateful for any help!
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan O'Neill wrote:However when I try to compile the Bob.java file with the same command it fails..


What exact command you did used and what was the compile error as well as source code of files, please mention also.
 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source file

Human.java



Bob.java



The command I enter to command prompt is:




Woops forgot the error:
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
classpath is missing that's why you got compile error as well as you don't need to write import statement because both classes are in same package.
 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in relevance I can combine both the -d and -cp option when compiling files?

How do I go about doing that?

Aha cheers for the info on the import statement!
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan O'Neill wrote:So in relevance I can combine both the -d and -cp option when compiling files?

Yes, Bob extends Human class and that is in classes folder not in source folder, so you have to mention that classpath when you do compile Bob.java.

 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok let me clear this up as im just confused now.
I wish to place the Bob.class in the classe\earth\human directory

What I don't understand is this: How do I combine both -d and -cp into one big statement to input into the cmd prompt?

When I use the this command:



I get the following error:



What am I doing wrong here? Is it the javac command?

I appreciate all the help Ankita thank you.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.Try this..

javac -cp classes -d source\earth\human\Bob.java

-hth
 
Jonathan O'Neill
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that worked thank you very much. Now to work out why that works! I can do that myself now and always come back to here if I have further questions.

Thank you again for your help Ankita.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something not quite right about public class Bob extends Human. I would have thought it might be public class Man extends Human or Human bob = new Man()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic