Organization of projects in packages

Asked

Viewed 725 times

2

I am doing a study on Java and would like to learn without using those Ides full of resources that do everything. After several attempts to learn how to use packages by creating them manually, one class is failing to see others outside the package. Follow a totally generic example:

The folder structure is as follows:

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

And the file codes are as follows::

-> Java test.

package pacotes;
import pacotes.objeto.*;

public class Teste{

    public static void main(String[] args) {

        Texto t = new Texto();
    }
}

-> Java text.

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 Test.java says the package 'packages.object' does not exist, which is wrong?

  • Are you using any IDE (Netbeans, Eclipse, etc) or are using text editor only (e.g., notepad)?

  • My intention is to avoid netbeans or eclipse as I want to learn manually

  • That’s what I thought... by the way you are trying to compile only the Test class. See my answer below showing how to compile the project.

  • I deleted the previous comment because I saw something I didn’t have in the accepted answer. Besides you said that your example is completely generic, so the fact that the Text class does not have a public constructor is probably not a problem in your real example, however I will fix it because the way it is does not work.

2 answers

4


By the way you are doing everything in text editor, IE, you are not using an IDE.

You need to specify the package to compile:

C:\Users\Leandro\Documents\Java\Exercícios\testes>javac pacotes/Teste.java
  • Note: See that you need to be outside the "packages" directory to compile.

  • Very cool, you saved me, like, now I can’t do a ...test/packages/java test

  • now, when I go to run a done project and packages, I have to compile all my classes without main by the normal method, and then compile the main one this way

  • 1

    You can build your base package: Ex: c: javac packages/*. java

  • really useful information

  • Still, it’s always best to use an IDE so you don’t have to worry about it.

Show 1 more comment

2

Don’t use wildcards this is *

with that you can make:

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 it is not protected and restricted to the package pacotes.objeto).

package pacotes.objeto;

public class Texto{

    public Texto(){
        System.out.println("Construiu um objeto");
    }
}
  • Damn, now reading your answer I think I shouldn’t have edited the question, but so the problem of not finding the class was a classpath problem, not the visibility of the constructor. To add, whether you use wildcard or not will not interfere with the result. The answer is to use classpath correctly. About my issue in question, if you want I can reverse (or you can yourself).

  • @Math thinks I have no reputation needed to reverse his editing. As for wildcards, it wasn’t the mistake he was finding more yet are not recommended

Browser other questions tagged

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