tags:

views:

226

answers:

5

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

  • In general, how can classes access one another in the same 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
+5  A: 

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.

Adrian Mouat
the last thing has the err: the path is wrong. I supposed it to be "SetWord.java" not "tools/setWord.java". I can solve by making the classes independent of one another in the tools-dir -- but is there some easier way to do it?
HH
Sorry, I don't understand what you mean here.
Adrian Mouat
Adrian Mouat: I have a package tools that has "scissor", "camera", "spell", "setWord", "sanitise" -- how can you access sanitise with the class "spell"?
HH
If they're both public classes, both in the "tools" package and both in the same directory, it should work.
Adrian Mouat
You've now edited the question substantially - it's really a different question. Try adding "." to the classpath though.
Adrian Mouat
@Adrian: 'Try adding "." to the classpath'?
HH
Try passing -cp "." to javac
Adrian Mouat
@Adrian: please, see the makefile. Even with the recent mod the same err
HH
And if you compile *.java rather than just PackTest.java?
Adrian Mouat
SOLVED: the err was "./", please, see the makefile. It should be very easy to replicate with the sources.
HH
+1  A: 

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.

Kevin Panko
No, you never need to import classes from the same package.
Michael Myers
+1  A: 

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.

Martijn Courteaux
This observation actually lead to the solution. Accepted because it is the CRUX move.
HH
You need to be one dir down. So you need to run commands like "$ javac ./pacc/PackTest.java; java pacc/PackTest", see the makefile.
HH
A: 

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.

egervari
A: 

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.

DaveG