Interpreting code with bundled classes

Asked

Viewed 46 times

0

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

I compiled normally 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 executing the java command, to interpret, the following message is appearing:

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 I am compiling packaged classes. For unpackaged classes performs normally

  • Did you ever read the other answers? It seems to me that this question has already been answered in your other question.

  • 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 of javac 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.

1 answer

3

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.

  • Thanks for the help. I managed to execute my code. I was using bar, in which case I would have to use the following structure.java -cp bin com.scja.Exam.teste.Imprimeplaneta. Thanks for the tip

  • Hi Marcia, how are you? Glad to have helped, here at Stack Overflow we signal good responses with positive votes, the answer that solved your problem can be marked as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.