Originally posted by Charlie Swanson:
How do I compile the below program given that the class Exercise5_2 is suppose to be in chatclient.
Do I need to make a directory, or will the this command create the directory automatically in addition to putting the class in the package.
Assume that the source c:/Chat/Exercise5_2.
Thanks in advance.
package chatclient;
import chat.*;
public class Exercise5_2 {
public static void main(String args[]) {
System.out.println("Main");
}
}
Your directory structure should match your package structure. Your file Exercise5_2.java needs to be in a directory named chatclient; that's the package you have defined Exercise5.2 to belong in.
Assuming you have C:\ as the root,
you should have a path : C:\chatclient\Exercise5.java.
Also, I don't know where your chat package is that you're importing. This directory structure I just showed you above has no such package named "chat", so that will be a problem when you compile.
So when you want to compile this class, your working directory should be C:\. Then you can type
javac -classpath ./ chatclient.Exercise5_2.java
This tells javac to use the current directory as the classpath. Since the classpath is set up correctly, javac can easily find your class chatclient.Exercise5_2.java, because you've told it where to find it.
To run your class, you need to specify the same classpath. Assuming that again C:\ is your working directory, you would type
java -classpath ./ chatclient.Excersice5_2
Just to plant a seed of information, at some point a java project gets so "big" that it becomes really really tedious to have to do all this manual compiling and classpath setting. That's when you start to use a batch file (if you're on windows ) or a script file on Unix. That way you can just double click your batch file to compile your project, or run your class. But this is just something for you to keep in mind.