Javac command with more than one packaged class

Asked

Viewed 504 times

3

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

I need to compile this code via command line, but I’m having difficulties.

Follow my attempts and error messages.:

Commando: 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 tried too without the option -cp, but it gives an error message on the lines where the objects were declared. It says that it did not find:

screenshot

  • 1

    Hi @Marcia, problem solved? If yes, don’t forget to mark an answer as correct (or reply yourself with the solution you found).

3 answers

2

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

C: Users marci_000 Documents OCA exercicios>javac -d bin -cp src com scja Exam planetas*. java;src com scja Exam teste Imprimeplaneta.java

I believe you forgot to put the " ". To correct, adjust 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
  • Thanks guys, problem solved. The error was in import.Thanks for the help.

  • @Marcialima you saw my answer?

2

You can just do:

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

The ./ reference to the root directory of your project.

When we want to compile more than one package, we can declare all 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, note that in your print, the first error message is in import.

Cannot import classes this way.

Or do you care class by class of package:

import com.scja.exam.planetas.Earth;

Or you matter the whole package with * in the end:

import com.scja.exam.planetas.*;

We call it import explicit and implicit (respectively).

1

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

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

And then, just run the compila.bat.

If you’re on linux, create a compila.sh:

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

And run that compila.sh.

It is important to note the following in these commands:

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

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

  • It’s a much more practical approach because you won’t need to manually add your source files and packages manually to the command line.

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

Browser other questions tagged

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