I'm trying to call the class Usuario in the class App. They are in the same package, the same folder, but still, the java compiler is can't find the class.
** I've cloned the code from a repository in github (don't know if that makes any difference).
package code;
import java.util.*;
/**
* App
*/
public class App {
public static void main(String[] args) {
// abre o scanner
Scanner teclado = new Scanner(System.in);
//inicializa alguns usuarios e departamentos
Usuario user1 = new Usuario("123", "joao", 1);
//variaveis do usuario logado
String id = null;
String nome;
int tipo;
String cargo;
//variavel do fim do programa
boolean fim_do_programa = false;
System.out.println("Sistema de Controle de Aquisicoes\n");
while(!fim_do_programa){
if(id == null){
// primeiro acesso deve requerir o login
}
// input do usuario
System.out.print("Digite o a função desejada: ");
int inputUsuario = teclado.nextInt();
switch(inputUsuario){
case 1 :// Mudar o usuario atual;
break;
case 0 : System.out.println("Fechando o programa...");fim_do_programa = true; break;
}
}
}
}
The other class in this example is the "Usuario"
package code;
import java.util.*;
public class Usuario {
private String id;
private Departamento departamento;
private String nome;
private static int countUser = 0;
private int tipo;
private String cargo;
public Usuario(String codigo, String nome, int tipo) {
this.id = codigo;
this.nome = nome;
if (tipo == 1 || tipo == 2) {
this.tipo = tipo;
} else {
System.out.println("ERRO AO CRIAR USUÁRIO " this.nome ". Usuário não será criado.\n");
this.tipo = 0;
}
countUser ;
}
public String getId() {
return id;
}
public static int getCountUser() {
return countUser;
}
public String getNome() {
return nome;
}
public int getTipo() {
return tipo;
}
public String getCargo() {
if (this.tipo == 1) {
this.cargo = "funcionário";
return this.cargo;
} else if (this.tipo == 2) {
this.cargo = "administrador";
return this.cargo;
} else {
this.cargo = null;
return this.cargo;
}
}
@Override
public String toString() {
return "Usuario [cargo=" cargo ", departamento=" departamento.getDepartamento() ", id=" id ", nome="
nome "]";
}
}
When i try to compile the code, this error keeps showing
PS C:\Users\thoma\OneDrive\Área de Trabalho\TrabalhoG\trabalhoGCS-PUCRS\code> javac App.java
App.java:14: error: cannot find symbol
Usuario user1 = new Usuario("123", "joao", 1);
^
symbol: class Usuario
location: class App
App.java:14: error: cannot find symbol
Usuario user1 = new Usuario("123", "joao", 1);
^
symbol: class Usuario
location: class App
2 errors
CodePudding user response:
I think you are just compiling it incorrectly.
Make sure that
App.java
,Usuario.java
andDepartamento.java
are all in a directory namedcode
. That is the name of the package.Change directory to the directory above the
code
directory.Compile as follows:
javac -cp . code/App.java
If the classpath / sourcepath are correct, then the Java compiler will find previously compiled classes and source files for any of the classes that the class you are compiling (e.g. code/App.java
) depends on. If both source and compiled (.class
) versions are found, the newer is used. That means that a newer source file be recompiled. So ... in your example ... code/Usario.java
and code/Departamento.java
should be compiled or recompiled.
For more information, read the documentation for the javac
and java
commands. Pay particular attention to the description of the classpath and source path and the javac
doc's section on "searching for types".
Having said that, for anything more complicated than this you should graduate to using a Java build tool such as Maven, Gradle or Ant.