I use cmd to compile my java applications, but when I use package to pack all the files, if I try to compile them in the working directory, even if I use full path of the file, that won't work. But when I go to the parent directory, the same command works. And only works in parent directory, children directories or other directories also won't work. Can somebody tell me why? or is there any solution that make javac work in the working directory, because I use Sublime Text, its builder configs and binds the working directory.
-
1javac expects you to honor the classpath. So you need to point whatever tool you are using to the root of your class paths, not at the java files themselves. – MeBigFatGuy Mar 20 '17 at 17:24
-
Try doing this: javac */*/*/*.java on Mac and Linux, but javac *\*\*\*.java on Windows – PRATHAP S Mar 20 '17 at 18:04
-
http://stackoverflow.com/a/5194940/3381825 – PRATHAP S Mar 20 '17 at 18:05
2 Answers
Read https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html to understand how Java source files should be organized. Espcially,
... put the source file in a directory whose name reflects the name of the package to which the type belong...
By default the source code for my.pkg.MyClass
must reside in current_directory/my/pkg/MyClass.java
. You can use other than current directory with javac option -sourcepath
.

- 2,465
- 1
- 24
- 45
For example you have two classes in two different packages packagea
and packageb
laying in the parent directory.
And class names are ClassA.java and ClassB.java respectively, and you have packaged your classes in these packages then you have
to use javac
command in parent directory like this:
javac packagea\ClassA.java packageb\ClassB.java
Your directory structure will be like this:
Parent_directory
|
|--packagea->ClassA.java
|--packageb->ClassB.java
Otherwise, you can use any build tool like ant
or maven
to compile
your code. You have to just run build.xml
file and your all files will
be compiled.

- 8,082
- 2
- 24
- 33

- 1
- 2