Organization of projects in packages

2

I'm doing a Java study and would like to learn without using those feature-rich IDEs that do it all. After several attempts to learn to use packages, creating them manually, one class is not able to see others outside the package. Here is a completely generic example:

The folder structure is as follows:

-pacotes
 Teste.java(classe com main)
 --objeto
   Texto.java

And the file codes are as follows:

- > Test.java

package pacotes;
import pacotes.objeto.*;

public class Teste{

    public static void main(String[] args) {

        Texto t = new Texto();
    }
}

- > Text.java

package pacotes.objeto;

public class Texto{

    public Texto(){
        System.out.println("Construiu um objeto");
    }
}

When compiling the Text.java file everything happens normally, but when compiling the Test.java speaks the package 'packages.object' does not exist, what's wrong?

    
asked by anonymous 21.07.2014 / 21:30

2 answers

4

By the way, you're doing everything in text editor, that is, you're not using an IDE.

You need to indicate the package to compile:

C:\Users\Leandro\Documents\Java\Exercícios\testes>javac pacotes/Teste.java
    
21.07.2014 / 21:54
2

Do not use wildcards ie *

With this you can do:

package pacotes;

import pacotes.objeto.Texto;


public class Teste{

    public static void main(String[] args) {

        Texto t = new Texto();
    }
}

Detail the Text constructor should be declared as public (if not as protected and restricted to pacotes.objeto ).

package pacotes.objeto;

public class Texto{

    public Texto(){
        System.out.println("Construiu um objeto");
    }
}
    
21.07.2014 / 21:39