About the build
The first thing you have to keep in mind is that src
is not part of the package structure, it is just the home directory that contains the source code of the application.
javac -d bin src\com\scja\exam\planetas\*.java src\com\scja\exam\teste\ImprimePlaneta.java
The src
directory is an excellent candidate for entering the path list where javac
looks for source code during compilation. This list is sourcepath .
The advantage of using sourcepath is that, knowing where the sources are, javac
is smart enough to find and compile transitively all .java
files needed to% to function. For example, if ImprimePlaneta
has ImprimePlaneta
:
import com.scja.exam.planetas.PlanetaEnum;
The
import
can find the file
javac
in the
PlanetaEnum.java
directory within some of the sourcepath entries:
That is, we can simplify the compilation to:
javac -d bin -sourcepath src src\com\scja\exam\teste\ImprimePlaneta.java
If sourcepath was not specified, com\scja\exam\planetas
uses classpath as sourcepath . So, you can also use the variation below:
javac -d bin -cp src src\com\scja\exam\teste\ImprimePlaneta.java
That being said, it is important to note that javac
is used to make -cp
files available to the application. Although a good project by convention does not have any .class
files in the .class
folder, it is worth leaving the intent to only search for explicit source files using src
.
After compiling, you will have a -sourcepath
directory with a structure similar to the following:
bin
└───com
└───scja
└───exam
├───planetas
│ PlanetaEnum.class
│
└───teste
ImprimePlaneta.class
Running the application
Notice that the bin
directory is a counterpart of bin
. As we have seen, src
can be used to specify paths in which the application looks for compiled units. So the correct way to run your application from the -cp
directory is:
java -cp bin com.scja.exam.teste.ImprimePlaneta
Note that the exercicios
command receives the qualified name of the class that should be executed. This makes sense because the class could, for example, be inside a jar.
As in the example of java
the javac
command is smart enough to find the classes used by java
in classpath .