Interpreting code with packaged classes

0

I have 3 classes in the com\scja\exam\planetas package. I also have a class with method main in pacote com\scja\exam\teste . The main method is responsible for printing the name of the planets.

I've usually compiled as follows:

C:\Users\marci_000\Documents\OCA\exercicios>javac -d bin -cp .\src\com\scja\exam\planetas\*.java .\src\com\scja\exam\teste\ImprimePlaneta.java  

But when you run the java command to interpret, the following message appears:

Erro: Não foi possível localizar nem carregar a classe principal .\bin\com\scja\exam\teste\ImprimePlaneta

The command used to interpret is as follows:

C:\Users\marci_000\Documents\OCA\exercicios>java -cp .\bin\com\scja\exam\planetas .\bin\com\scja\exam\teste\ImprimePlaneta

I checked the environment variables and they are ok. And this error only occurs when compiling packaged classes. For unpackaged classes it runs normally

    
asked by anonymous 28.02.2017 / 09:42

1 answer

2

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 .

    
28.02.2017 / 13:40