0

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.

A-yon Lee
  • 2,074
  • 2
  • 9
  • 18

2 Answers2

1

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.

haba713
  • 2,465
  • 1
  • 24
  • 45
0

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.

AP.
  • 8,082
  • 2
  • 24
  • 33