javac command with more than one packaged class

2

I have 3 classes in the com\scja\exam\planetas package. I also have a class with method main in package com\scja\exam\teste , responsible to print the name of the planets.

I need to compile this code via the command line, but I'm having trouble.

Here are my attempts and error messages:

  

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

     

Error message: javac: no source files Usage: javac <options> <source files> use -help for a list of possible options

I also tried without the -cp option, but it gives an error message on the lines where the objects were declared. It says that it did not find:

    
asked by anonymous 22.02.2017 / 10:09

3 answers

1
Note that you are only compiling JAVA files that begin with the name planets (planets * .java):

  

C: \ Users \ marci_000 \ Documents \ OCA \ exercises \ javac -d bin -cp src \ com \ scja \ examines \ planets * .java; src \ com \ scja \ exam \ test \ PrintPlaneta.java

I think you forgot to put the "\". To fix, set to:

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

You can only do:

javac ./com/scja/exam/planetas/*.java ./com/scja/exam/teste/*.java

./ refers to the root directory of your project.

When we want to compile more than one package, we can declare all of these packages by separating them by space.

By doing so, you compile all classes within com/scja/exam/planetas and all classes within com/scja/exam/teste

Another thing, notice that in your print, the first error message occurs in import .

You can not import classes that way.

Or you import class by package class:

import com.scja.exam.planetas.Earth;

Or you import the entire package with * at the end:

import com.scja.exam.planetas.*;

We call this explicit% and implicit (%).

    
22.02.2017 / 12:22
1

If you are on windows, create a compila.bat file with the following:

cd src
dir /s /B *.java > ../sources.txt
cd ..
javac -d bin @sources.txt

And so, just run compila.bat .

If you are on linux, create a compila.sh :

cd src
find -name "*.java" > ../sources.txt
cd ..
javac -d bin @sources.txt

And execute this compila.sh .

It is important to note the following in these commands:

  • The -cp flag is for you to indicate where the previously compiled classes will be used as dependencies / libraries. Therefore, it is not the flag you want.

  • This command works by listing all the source files for your project, creating a list of them in a file, and sending javac to compile the files that are in the list.

  • >
  • It's a much more practical approach because you will not have to manually add your source files and packages manually to the command line.

  • Works for any number of source and package codes that exist in your src folder.

22.02.2017 / 12:11