On the compilation
The first thing you have to keep in mind is that src
is not part of the package structure, it is only the base 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 directory src
is an excellent candidate to enter the list of paths in which the javac
find source code during build. This list is sourcepath.
The advantage of using the sourcepath is that, knowing where the sources are, javac
is smart enough to find and compile transitively all files .java
necessary to make ImprimePlaneta
function. For example, if ImprimePlaneta
has a import
:
import com.scja.exam.planetas.PlanetaEnum;
The javac
knows how to find the file PlanetaEnum.java
in the directory com\scja\exam\planetas
within some of the entrances of the sourcepath:
That is, we can simplify the compilation to:
javac -d bin -sourcepath src src\com\scja\exam\teste\ImprimePlaneta.java
If the sourcepath was not specified the javac
uses the value of classpath as sourcepath. Therefore, it is also possible to use the variation below:
javac -d bin -cp src src\com\scja\exam\teste\ImprimePlaneta.java
That said it is important to note that -cp
is used to make files available .class
for the application. Although a good draft by convention has no file .class
in the briefcase src
, it is worth leaving the intention to search only explicit source files using -sourcepath
.
After the compilation you will have a directory bin
with a structure similar to the following:
bin
└───com
└───scja
└───exam
├───planetas
│ PlanetaEnum.class
│
└───teste
ImprimePlaneta.class
Running the application
See that the directory bin
is a counterpart of src
. As we saw the -cp
can be used to specify paths in which the application searches for compiled units. Soon the correct way to run your application from the directory exercicios
is:
java -cp bin com.scja.exam.teste.ImprimePlaneta
Note that the command java
receives the qualified name of the class that must be executed. This makes sense as the class could, for example, be inside a jar.
As in the example of javac
the command java
is intelligent enough to find the classes used by ImprimePlaneta
in the classpath.
Possible duplicate of Javac command with more than one packaged class
– user28595
Did you ever read the other answers? It seems to me that this question has already been answered in your other question.
– user28595
Hi @diegofm, sorry, I had not seen the duplicate question when I answered. Although the question now, from the point of view of OP is on the side of
java
instead ofjavac
I ended up repeating many things that were answered in the other question. Anyway I’ll leave the answer here for now since there it would be slightly outdated.– Anthony Accioly