• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package Question II

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic