hhh
hhh

Reputation: 52840

Java newbie problem: classes of the same package accessing one another?

The classes belong to the same pkg. They are in the dir, name of the pkg.

Error

javac PackTest.java
PackTest.java:8: cannot find symbol
symbol  : class PriTest
location: class pacc.PackTest
  System.out.println(new PriTest().getSaluto());
                         ^
1 error

Classes in the PKG pacc

$ cat PackTest.java 
package pacc;
import java.io.*;

public class PackTest
{
    public static void main(String[] args)
    {
        System.out.println(new PriTest().getSaluto());
    }
}
$ cat PriTest.java 
package pacc;

public class PriTest
{
    public PriTest(){}
    private String saluto="SALUTO FROM PriTest";
    public String getSaluto(){return saluto;}
}

PKG of the name of dir

$  find .. -type d -name "pacc"
../pacc
$ ls ../pacc 
makefile  PackTest.java  PriTest.java
$ ls
makefile  PackTest.java  PriTest.java

Solved!

$ cat makefile 
p:
    javac ./pacc/PackTest.java
    java pacc/PackTest
$ make p
javac ./pacc/PackTest.java
java pacc/PackTest
SALUTO FROM PriTest

Upvotes: 4

Views: 5103

Answers (5)

DaveG
DaveG

Reputation: 301

Once you master the fundamentals, an IDE is an essential productivity tool.

Until then, it's good for you to think through these problems to gain a deeper understanding of Java development.

As far as ANT, it is worth your time to move to that now. Makefiles don't offer you any further edification of programming.

Upvotes: 0

egervari
egervari

Reputation: 22512

Get an ide like IntelliJ IDEA. Java is so complex and bloated this will help so you need not even bother with such trivial things.

Upvotes: 0

Kevin Panko
Kevin Panko

Reputation: 8534

If you are used to the C and C++ approach of compiling each file separately, you may be surprised to learn that the Java compiler works best when you give it your entire project to compile all at once.

Apache Ant is a commonly used tool for building Java projects. It does the job of calling javac for you. It works better for Java than make(1) does.


Compile as:

H:\test\so>javac pacc/PackTest.java pacc/PriTest.java

You should be in the root of your project, not in the folder pacc.

Upvotes: 1

Adrian Mouat
Adrian Mouat

Reputation: 46500

Make sure the files are in the same directory, with the same name as the package. Also, make sure the classpath is set correctly.

Packages mimic the directory structure - "Test.java" in package "org.example.test" should be found at "org/example/test/Test.java".

The following compiled your files for me:

 $ javac -cp "." *.java

And I ran with

 $ cd ..
 $ java pacc.PackTest

No problems here.

BTW Apache Ant is generally preferred over makefiles in the Java universe.

Upvotes: 6

Martijn Courteaux
Martijn Courteaux

Reputation: 68857

MAYBE (So don't kill me, please) this is a solution:

In the terminal, go to the root of your java project (so the default package, in your situation, the parent directory of the folder pacc).
Then type: javac pacc.PackTest.java

I don't use the compiler manually. My IDE does that work for me.

Upvotes: 2

Related Questions