Compile multiple java files in the same folder

Asked

Viewed 3,435 times

4

I made a very simple Java program, using the notepad and compiling by CMD. The problem is that even though the files are in the same folder, the class that has the method main() does not compile.

Follow the code below:

Main class

public class Main {
    public static void main(String[] args) {
        Nada nada = new Nada();

        nada.showVars();
        nada.setName("maisnada");
        nada.setN(16);
        System.out.println();
        nada.showVars();
    }
}

Classe Nada

public class Nada {
    private String nome;
    private int numeroQualquer;

    public Nada() {
        this.nome = "nada";
        this.numeroQualquer = 3;
    }

    public void setName(String nome) {
        this.nome = nome;
    }

    public void setN(int n) {
        this.numeroQualquer = n;
    }

    public void showVars() {
        System.out.println(nome);
        System.out.println(numeroQualquer);
    }
}

When I try to compile Main.java, the following error occurs in CMD:

Main.java:3 error: cannot find symbol
               Nada nada = new Nada();
               ^
   symbol:   class Nada
   location: class Main
Main.java:3 error: cannot find symbol
               Nada nada = new Nada();
                               ^
   symbol:   class Nada
   location: class Main
2 errors

The two files, Main.java and Nada.java, are in the same folder, and Nada.java compiled normally.

EDIT:

I managed to solve the problem, it was the environment variable CLASSPATH, she was like %JAVA_HOME%\lib instead of .;%JAVA_HOME%\lib.

The command I used to compile was this:

javac Main.java

Thank you to all who responded.

  • 1

    What command did you use to compile? You checked the current folder - . - is in the CLASSPATH? For example: javac -cp . Main.java

  • I used javac Main.java

  • 2

    Edith your question and add the command you are using to compile.

  • You are using packages?

  • No, but I already solved, it was the same CLASSPATH. It was worth the help.

2 answers

5

You need to compile all the files .java and not just the Main. Since you did not put which command you are using to compile, I assume this is your mistake

To do this you can run the command:

javac *.java

The * is a wildcard very common which means all or all. In that case the *.java that is to say all the files .java in this folder.

0

Just do the following command:

javac diretorio/*.java

Browser other questions tagged

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