aspose file tools*
The moose likes Beginning Java and the fly likes how to compile java file that implements an interface and put it in different folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to compile java file that implements an interface and put it in different folder" Watch "how to compile java file that implements an interface and put it in different folder" New topic
Author

how to compile java file that implements an interface and put it in different folder

Rod Taylor
Greenhorn

Joined: Aug 03, 2012
Posts: 17

Hi,

I am having a problem in compiling java files that implements an interface. I also want to put the source files (.java extension) and the class files (.class extension) on different folders.

I keep getting error "cannot find symbol" on MyCar.java everytime i try compiling it. Here is my source code:

Car.java:

package interfacePractice;

public interface Car {
//constants definitions
int minimumSize = 25;

//method signatures
int turn (double radius, double speed);
int signalTurn(boolean signalOn);
//String printMe;
}

MyCar.java:

package interfacePractice;

public class MyCar implements Car {
int Turn(double radius, double speed) {
// Definition of method Turn
}

int SignalTurn(boolean signalOn) {
// Definition of method SignalTurn
}

public MyCar() {
System.out.println("An object of MyCar has been created. The min size is: " + minimumSize);
}

public String printMe() {
return "I am a VW";
}
}

MainApp.java:

package interfacePractice;

public class MainApp {
public static void main(String[] args) {
MyCar aCar = new MyCar();
System.out.println(rodCar.printMe);
}
}

I use the command: javac -d C:\java\class MyCar.java when I try to compile MyCar.java. It went ok with Car.java but not with MyCar.java. Can anyone help please? Thank you.


Nothing you're really good at that you don't do everyday (Will Smith)
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

check your classpath settings here and try to compile again.


Thanks,
Tushar (SCJP 1.5)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi Rod

Welcome to the forum.

Now , before I begin to answer your question, a few pointers as to how one should go about posting questions.

a) Always use code tags to paste code. Properly indent the code.

See how much neater and legible the code becomes :



Secondly, method names always start with a lower case alphabet and from then on follow "camelCasing". Look carefully at your code. You have mistakenly changed the case of the interface methods and implementing class methods. Even if you get through the first hurdle, your MyCar.java will not compile. Also put proper return statements while implementing these methods in MyCar.java.


Now to get back to your question. What is the command that you are giving to the java compiler when asking it to compile MyCar.java. Tell me the exact command.



~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
And please show us the directory structure. Which is your current directory?
Rod Taylor
Greenhorn

Joined: Aug 03, 2012
Posts: 17

Mansukhdeep Thind wrote:


Now to get back to your question. What is the command that you are giving to the java compiler when asking it to compile MyCar.java. Tell me the exact command.




First off, thank you very much for your corrections. You really made a point with clarity of the code.

Second, the command I use is to compile MyCar.java and to compile MainApp.java. I use this command (-d) because I want to put the .class files and .java files in two separate folders.

Campbell Ritchie wrote:
And please show us the directory structure. Which is your current directory?


C:\javacodes have 2 folders, namely class and source. I want to put all the .java files in source folder and the class files in class folder. At the moment, I have all the MyCar.java, Car.java and MainApp.java in source folder.My current directory, where I'm trying to compile all the java files from, is C:\javacodes\source directory on the command prompt. But I can only seem to be able to compile Car.java. I can't compile the other two files from the same location. It keeps giving out error of "Cannot find symbol" on both the other classes.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Rod,

Did you happen to set the CLASSPATH environment variable, if not you may want to add that option to your compilation (so it knows where to look for the .class files you need).

-steve
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I have resolved this issue at my end. But I won't be providing you with the answer straightaway. I would like you to understand how the "javac" command searches for dependent compiled class files.

At the moment, I have all the MyCar.java, Car.java and MainApp.java in source folder.My current directory, where I'm trying to compile all the java files from, is C:\javacodes\source directory on the command prompt. But I can only seem to be able to compile Car.java. I can't compile the other two files from the same location. It keeps giving out error of "Cannot find symbol" on both the other classes.


This is happening because the java compiler is unable to locate the compiled interface Car.class file. Study in detail the following-

a) -classpath flag option of javac command

b) significance of package statement in relation to javac command

c) how the compiler searches for classes i.e. what all paths / directories will it look in. You do have the compiled Car.class file. But the compiler is unable to locate it. Find out the reason why.

You find answers to all these questions, you find the answer to your posted query.

Rod Taylor
Greenhorn

Joined: Aug 03, 2012
Posts: 17

Mansukhdeep Thind wrote: Study in detail the following-

a) -classpath flag option of javac command

b) significance of package statement in relation to javac command

c) how the compiler searches for classes i.e. what all paths / directories will it look in. You do have the compiled Car.class file. But the compiler is unable to locate it. Find out the reason why.

You find answers to all these questions, you find the answer to your posted query.



Many thanks for your reply. I finally figured it out. It was all because the compiler cannot locate the .class file. By providing the -cp on the javac command (in conjuntion with -d option), it overrides the CLASSPATH environment variable, or the current directory if it's not set.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compile java file that implements an interface and put it in different folder