Compiling packages from command line
Hi. I'm new to Java. I have a custom class in a package and my main class is in the default unnamed package. Whenever I compile my main class, it doesn't recognize my class, although I've imported the package. After some poking around, I was able to figure out that I need to have the .java file for the package classes in their respective directory trees for the program to compile. What I want to know is, is this correct. If so, do I have to manually create all the directories and then put the .java files in them? Isn't there any way I can do it automatically? Thanks.
Use javac -d .; It should take care of the directory structure while compiling. :)
Nope. Doesn't help. I don't think you understand the problem. javac -d works when I'm compiling a class that's part of a package. You see, my main class is part of the unnamed package while it's calling another user-defined class that is part of a package. The problem arises if at compile-time, the .java file of the utility class does not exist in the proper directory tree of the classpath.If it does, then the main class compiles fine and the utility class is also automatically compiled. Tell me if you understand the problem now.
> Nope. Doesn't help. I don't think you understand the
> problem. javac -d works when I'm compiling a class
> that's part of a package. You see, my main class is
> part of the unnamed package while it's calling
> another user-defined class that is part of a package.
> The problem arises if at compile-time, the .java file
> of the utility class does not exist in the proper
> directory tree of the classpath.If it does, then the
> main class compiles fine and the utility class is
> also automatically compiled. Tell me if you
> understand the problem now.
If you have source code files in the Classpath when you use the javac compiler, it will find and compile them when needed. If the source code files are not in the Classpath, javac will not find them. That is the way javac works.
You can specify more than one .java file when you compile, or use the -sourcepath option, so if you want the source code in a different place from the compiled .class files, you can do that as well.
You still don't understand my problem. Suppose I create objects of 20 different user-defined classes in my main function. I already know the .java files need to be in the classpath. The thing is, if all the classes are from different packages, do I need to create directories for all the 20 packages and put the .java files in them, in order to compile my program, or is there some automatic way to do it?
>I already know the .java files
> need to be in the classpath.
Actually, only the .class files need to be in the Classpath. If .java source code files are in the Classpath, the javac compiler will automatically compile them when it needs them, to create a suitable .class file. If there are no .java files, then a suitable .class file must exist.
> The thing is, if all the
> classes are from different packages, do I need to
> create directories for all the 20 packages and put
> the .java files in them, in order to compile my
> program, or is there some automatic way to do it?
Both the compiler and the JVM need the .class files. If you have 20 packages then you need 20 directory structures that match the packages. The .class files must be in the proper directory structure.
I do not use an IDE, but I believe that IDE's generally provide an 'automatic' way to do it.
The -d option also creates the directories you need, but that didn't seem to be what you wanted.