aspose file tools*
The moose likes Beginning Java and the fly likes Package Question II Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Question II" Watch "Package Question II" New topic
Author

Package Question II

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
I have another question regarding packages
Please consider this code:

package com.peter.foo;
public class Bar {
public void invoke() {
System.out.println("Invoking Bar.invoke()-Methode");
}
public static void main(String[] argv) {
System.out.println("Invoking Bar.main()");
}
}
Compiled with statement: „javac -d c:\jdk1.3\demo\packages Bar.java“
 Compiled package Bar.class was put into directory
„c:\jdk1.3\demo\packages\com\peter\foo“.
Now I want to call method „invoke() from program Bartest.java
import com.peter.*;
class Bartest {
public static void main (String args[]) {
Bar b1 = new Bar();
b1.invoke();
}
}
When I compile Bartest.java I receive error messages as follows:
> cd c:\jdk1.3\bin
> javac Bartest.java

c:\jdk1.3\bin>javac Bartest.java
Bartest.java:2: package com.peter does not exist
import com.peter.*;
^
Bartest.java:6: cannot resolve symbol
symbol : constructor Bar ()
location: class Bar
Bar b1 = new Bar();
^
Bartest.java:7: cannot resolve symbol
symbol : method invoke ()
location: class Bar
b1.invoke();
^
3 errors
It seems that Bartest.java cannot locate the package com.peter.foo
WHY? What must I do to properly compile this program?
Thanks for your answers.
Thomas
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Hi,
you can't expect the java compiler to recurse into a package tree from a starting point.
Putting a joker in an import thus just means:
import all the classes in this package
For example, if you write a statement like
import com.peter.*;
then the compiler will only look for a package named "com.peter", atd for any subpackages. You have not such it is named "com.peter.foo".
If you want to import classes in both com.peter and com.peter.foo, then you'll have to write
import com.peter.*;
import com.peter.foo.*;
W.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello Wilfried,
your advice helps but doesn't solve the problem. If I change the line from "import com.peter.foo" to "import com.peter.foo.*" the error that the package is not found still shows up.
I think I have to set my classpath so that the package is found. Can anyone tell me how to set the classpath properly?
Thanks.
Thomas
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
Thomas,
You might try adding "c:\jdk1.3\demo\packages;" to your CLASSPATH environment variable, if you haven't already. My guess would be that, since your second class is saved in the default package of a completely different location, it's looking for other packages in that location, rather than under c:\jdk1.3\demo\packages.
g.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
I went to the DOS-Prompt "c:\jdk1.3\bin>"
and stated:
set classpath = .;c:\jdk1.3\demo\packages
as you adviced me.
Then I compiled the application with
c:\jdk1.3\bin> javac Bartest.java
I received the same errors as before:
c:\jdk1.3\bin>javac Bartest.java
Bartest.java:1: package com.peter.foo does not exist
import com.peter.foo.*;
^
Bartest.java:5: cannot resolve symbol
symbol : constructor Bar ()
location: class Bar
Bar b1 = new Bar();
^
Bartest.java:6: cannot resolve symbol
symbol : method invoke ()
location: class Bar
b1.invoke();
^
3 errors
=> The compiler states that the package doesn't exist. The package exists and can be started with
c:\jdk1.3\demo\packages> java com.peter.foo Bar
Then it prints the result of Bar.java:
> cd c:\jdk1.3\demo\packages
> java com.peter.foo.Bar

c:\jdk1.3\demo\packages>java com.peter.foo.Bar
Invoking Bar.main()

=> This proves that the package exists but when I want to compile Bartest.java it doesn't find the package.
Please help me and tell me how I can get the compiler to find package com.peter.foo.* which I want to import into Bartest.java
Thank you very much for your answer.
Thomas
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
From my experience, setting environment variable is prone to error.
Here you should not have written
set classpath = .......
but

set CLASSPATH=.;d:\jdk1.3\demo\packages
-> Capital letters and no space between the 'H' and the '='.
This should solve the problem. The other solution is to use the -classpath option in the javac command.
W.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello Wilfried,
thanks for your advice. It helped. Now the package is found.
But there are still two errors:
Bartest.java:5: cannot resolve symbol
Symbol: constructor Bar()
Location: class Bar
Bar b1 = new Bar()
^
Bartest.java:5: cannot resolve symbol
Symbol: method invoke()
Location: class Bar
b1 .invoke()
^
Another problem occurred:
Having set the CLASSPATH as you told me, I want to start the compilation in my editor (PFE – Programm File Editor) because I get the output in my editor then. It seems as the editor ignores the CLASSPATH and states the error: Package doesn’t exist.
When I comile Bartest.java from the DOS-Windows (instead from the editor) it remembers the classpath and finds the package, resulting in only the two errors stated above.
What can I do that my editor remembers the CLASSPATH when I start the compilation from my editor instead of the DOS prompt?
Thanks for your answer.
Thomas
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Weird!
I have tried with the same environnement as yours.
So what is what I have got:
The same code as you.
Bar.java is in D:\jdk1.3\demo\packages
I compile with
D:\jdk1.3\demo\packages> javac Bar.java
Then
Bar.class is generated in D:\jdk1.3\demo\packages\com\peter\foo
Bartest.java is in D:\jdk1.3\bin
D:\jdk1.3\bin>javac -classpath d:\jdk1.3\demo\demo Bartest.java
Two things happens:
When Bartest starts with
import com.peter.foo.*;
it does not compile
When Bartest start with
import com.peter.foo.Bar;
It does compile.
Now if you move Bar.java in
D:\jdk1.3\demo\packages\com\peter\foo
both compile.
So, it seems that
import com.peter.foo.Bar is expecting to find the .class in the classpath
import com.peter.foo.* is expecting to find the .java in the classpath.
Very Strange indeed. Anyone noticed that?
Anyway, you should put the sources files directly in a directory structure which maps your package structure.
W.
[ July 11, 2002: Message edited by: Wilfried LAURENT ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

yes -- this has been discussed before -- maybe I'll try to search for the links in a bit.
The problem is that you have defined class Bar in a specific package (com.peter.foo.Bar). But Bartest is in the default package, and so using the generic import statement, com.peter.foo.* doesn't work.
If you pout Bartest in some kind of package (com, or even com.blah -- whatever) things will work like you expect them to.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello Jessica,
I don't think that your statement with the default package is correct.
I moved the Packages away from the default package from c:jdk1.3\bin to
c:\Java\EigeneJavaProgramme\eigene_packages;
I still cannot use "import com.peter.foo.*"
WHY?
Bartest with „import com.peter.foo“ doesn’t compile
import com.peter.foo.*;
class Bartest {
public static void main (String args[]) {
Bar b1 = new Bar();
b1.invoke();
}
}

> cd C:\Java\EigeneJavaProgramme\
> "C:\Java\EigeneJavaProgramme\1start.bat"

C:\Java\EigeneJavaProgramme>"C:\Java\EigeneJavaProgramme\1start.bat"
C:\Java\EigeneJavaProgramme>set PATH=.;c:\Java\SUNJava2SDKv1.4\bin
C:\Java\EigeneJavaProgramme>set CLASSPATH=.;c:\Java\EigeneJavaProgramme;c:\Java\EigeneJavaProgramme\eigene_packages;
C:\Java\EigeneJavaProgramme>javac Bartest.java
Bartest.java:5: cannot resolve symbol
symbol : constructor Bar ()
location: class Bar
Bar b1 = new Bar();
^
Bartest.java:6: cannot resolve symbol
symbol : method invoke ()
location: class Bar
b1.invoke();
^
2 errors

Bartest.java with „import com.peter.foo.Bar“ compiles without error
import com.peter.foo.Bar;
class Bartest {
public static void main (String args[]) {
Bar b1 = new Bar();
b1.invoke();
}
}
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
What Jessica meant by "default package" is that you do not have any 'package' statement in Bartest.java. So it belongs to the "default package".
So if you are to put Bartest.java in c:\Java\EigeneJavaProgramme\eigene_packages;
then put the statement
package eigene_packages;
as the topline of Bartest.java and add c:\Java\EigeneJavaProgramme to your classpath.
W.
Ps: See also this thread
[ July 15, 2002: Message edited by: Wilfried LAURENT ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Question II