s sivaraman wrote:i'd like to know how to compile a java file from different root directory.
i tried -classpath option but it doesn't seem to be working .
Let's take your source code file from one of your other topics
Let's assume the source code file is located in the directory
D:\ocajp\src\com\pack (as you can see I'm working on a Windows machine).
Now I can compile this source code file from the root directory
D:\ using
After executing this command a
Sample.class file is created in the directory
D:\ocajp\src\com\pack. Now if you want to execute this
.class file, you'll need to set the classpath to the appropriate directory
If you use a wrong directory in the classpath, the JVM won't be able to find your
.class file and you'll get an appropriate error message
Now if you want to seperate your source code files from the generated
.class files (which is considered to be a good practice), you can use the option
-d of the
javac command. So if we want to have the
.class files being generated in the directory
D:\ocajp\classes, you'll need to execute this command
As you can see from this error message, the destination directory (in this example:
D:\ocajp\classes) must exist! It's not created by the
javac command. So after I have created the directory
D:\ocajp\classes manually, I can run the previous command again
It finishes without an error message and a
Sample.class file is created in the directory
D:\ocajp\classes\com\pack. Now if you want to execute this
.class file, you'll need to set the classpath to the appropriate directory
Hope it helps!
Kind regards,
Roel