import statement in jsdk 1.4.2

L

Luke

Hi,

I'm having problems compiling a class that uses another class that is in
the same directory.

Apparently 1.4.2 cannot import straight classes, but must have a package
name.
I have placed the classes I need in a package but one of the classes in
the package needs another class that is in the package as well.
All sounds ok, but....

I have a class (a) in say /dir1/ which accesses a class (b) in /dir1/dir2
and a class (c) in either /dir1/dir2/ or /dir1/dir2/dir3
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

How do I work around this?

Please help me out, I'll be able to explain it better.
thanks,
kind regards,
Luke
 
M

Marek Lange

Luke said:
I'm having problems compiling a class that uses another class that is in
the same directory.

Apparently 1.4.2 cannot import straight classes, but must have a package
name.

If the class is in the same directory, no import is needed. You have to
use an import statement if you reference a class in another directory
(it is a different package then).
I have placed the classes I need in a package but one of the classes in
the package needs another class that is in the package as well.
All sounds ok, but....

I have a class (a) in say /dir1/ which accesses a class (b) in /dir1/dir2
and a class (c) in either /dir1/dir2/ or /dir1/dir2/dir3
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

How do I work around this?

Check Java tutorials for packaging.

-marek
 
K

KC Wong

I have a class (a) in say /dir1/ which accesses a class (b) in /dir1/dir2
and a class (c) in either /dir1/dir2/ or /dir1/dir2/dir3

How can class (c) be in *either* directory?

Class (c) is either:
package dir1.dir2; or package dir1.dir2.dir3;

It *cannot* be both.
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

I think you're completely messed up about Java package concept.


If I get it right, your class (a) should be:

package dir1;
import dir1.dir2.dir3;
class A {
....
}

Class (b) should be:
package dir1.dir2;
import dir1.dir2.dir3;
class B {
....
}

and class (c):
package dir1.dir2.dir3;
class C {
....
}

While the CLASSPATH environment variable points to the directory one level
up from dir1.

I might be wrong, because I don't understand your question/scenario at all.


KC.
 
D

Daniel Huesch

The package structure should be the same as the directory structure. Classes
within package dir1 should be marked as a part of package dir1 and vice
versa.
Apparently 1.4.2 cannot import straight classes, but must have a package
name.

Why do you think 1.4.2 cannot import straight classes? What exactly do you
think needs a package name?
I have a class (a) in say /dir1/ which accesses a class (b) in /dir1/dir2
and a class (c) in either /dir1/dir2/ or /dir1/dir2/dir3
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

If class b is really in the same directory as class c, why is there to
possibility for class c to be in either directory /dir1/dir2 OR
/dir1/dir2/dir3?

I tried the following and it works:

package dir1;
import dir1.dir2.*;
import dir1.dir2.dir3.*;

public class A {
public A() {
}
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
C c1 = new C();
}

}
package dir1.dir2;
import dir1.dir2.dir3.*;

public class B {
public B() {
}
public static void main(String[] args) {
B b1 = new B();
C c1 = new C();
}

}
package dir1.dir2.dir3;

public class C {
public C() {
}
public static void main(String[] args) {
C c1 = new C();
}

}

A.java is in <project-source-dir>/dir1
B.java is in<project-source-dir>/dir1/dir2
C.java is in <project-source-dir>/dir1/dir2/dir3
 
N

Neomorph

Hi,

I'm having problems compiling a class that uses another class that is in
the same directory.

Apparently 1.4.2 cannot import straight classes, but must have a package
name.

In Java, the directory from the classpath (or working directory) IS the
package name, which also needs to be declared in the source.
If you put the source file in the directory of its package, then the
compiler can find the source automatically if it is out of date or not
compiled yet.
Otherwise you have to compile the sources in the correct order yourself.
I have placed the classes I need in a package but one of the classes in
the package needs another class that is in the package as well.
All sounds ok, but....

I have a class (a) in say /dir1/ which accesses a class (b) in /dir1/dir2
and a class (c) in either /dir1/dir2/ or /dir1/dir2/dir3

A single class can only reside in one package.
A same named class can exist in another package, but does not neccessarily
contain the same code.

So now you have:
class dir1.(a) from source (a).java in any directory
class dir1.dir2.(b) from source (b).java in any directory
class dir1.dir2.(c) from source (c).java in directory dir1\dir2
class dir1.dir2.dir3.(c)
from source (c).java in directory dir1\dir2\dir3

(a), (b) and (c) are just the names. They all have different source code
(even (c), since the package declaration is different).

Since both (c) classes are public (since (a) can use either one), they need
to be in files with exactly the same name, but in different locations.
Using the package directories for the location makes the compiler find the
source automatically if a class-to-be-compiled needs it and the class does
not exist or is out of date, so the compiler can recompile that class.
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?

When using dir1.dir2.dir3.(c) from any class in dir1.dir2, you need to
specify dir1.dir2.dir3.(c) for each usage of (c) out of package
dir1.dir2.dir3, for example:

package my.package ;
public class Date {
import java.util.* ;

public myMethod() {
java.util.Date date = new java.util.Date(); // correct
Date date2 = new Date(); // compiler does not know which Date to
// take: the one in this package (itself), or the one in java.util.
}
}
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

(a) can use either one, as long as (a) uses the fully qualified name
(including the package name in front of the clsas name).
How do I work around this?

Please help me out, I'll be able to explain it better.
thanks,
kind regards,
Luke

Just use fully qualified names for different classes (i.e. classes from
different packages) with the same name.

Cheers.
 
T

Tor Iver Wilhelmsen

Luke said:
Apparently 1.4.2 cannot import straight classes, but must have a package
name.

Well, all imports require a package name, yes.
then I have a class (b) in /dir1/dir2 which accesses a class (c) in its same
directory (/dir1/dir2) or /dir1/dir2/dir3. This doesn't work in 1.4.2. Why?

It works. Are you sure you compile from the correct root?
I can't place (c) in /dir1/dir2/dir3 because it is also acessed by (a)

Why not? The package of (c) will be dir1.dir2.dir3 whether you import
it from (a) or (b). You only ever import with fully qualified package
names, never "subpackages".
Please help me out, I'll be able to explain it better.

So why didn't you explain it better from the beginning?
 
R

Roedy Green

Apparently 1.4.2 cannot import straight classes, but must have a package
name.

You don't need to import classes in the same package as the current
one.

The new thing with 1.4.2 is that the rule you may not include
packageless classes from packaged classes is now being enforced.

see http://mindprod.com/jgloss/import.html

The rule of thumb is, except for a one shot experiment class that uses
no other classes, put every class in a package.
 
L

Luke

Hi,

Thanks for your input :)

I've narrowed the problem down to make it clearer:

I created a 'test' directory, placed 2 classes in there.
The code is short and as follows:

#####################################
package test;

public class Pref
{
// attributes
private String reason;

// constructor method
public Pref()
{
}

// methods
public String getReason()
{
return reason;
}

public void setReason(String reason)
{
this.reason = reason;
}
}
####################################
package test;

public class PrefBean
{
// constructor method
public PrefBean()
{
Pref p = new Pref();
}

}
####################################

I can compile Pref.java, no probs
When compiling PrefBean.java the following error occurs:

[luke@knife test]$ javac PrefBean.java
PrefBean.java:8: cannot resolve symbol
symbol : class Pref
location: class test.PrefBean
Pref p = new Pref();
^
PrefBean.java:8: cannot resolve symbol
symbol : class Pref
location: class test.PrefBean
Pref p = new Pref();
^
2 errors
====================================
I can compile both classes if I don't include the 'package' statement
but I need to use these classes from JSPs, which only compile if the classes
are in packages,

So why don't they compile?
What am I doing wrong?
kind regards,
Luke
 
N

Neomorph

Hi,

Thanks for your input :)

I've narrowed the problem down to make it clearer:

I created a 'test' directory, placed 2 classes in there.
The code is short and as follows:

I can compile Pref.java, no probs
When compiling PrefBean.java the following error occurs:

[luke@knife test]$ javac PrefBean.java

Oops! You're in the wrong directory.
You shouldn't be in the directory that the Pref.class file is in, since
Pref.class has package test, you need to be in the directory that test is
in.

When javac looks for a compiled file, it needs them to be in the directory
structure that is equal to the package names from the current directory.

So do a "cd .." first and then "javac test\PrefBean.java"
PrefBean.java:8: cannot resolve symbol
symbol : class Pref
location: class test.PrefBean
Pref p = new Pref();
^

2 errors
====================================
I can compile both classes if I don't include the 'package' statement
but I need to use these classes from JSPs, which only compile if the classes
are in packages,

packages == directory structure, you need to be in the root of that
structure for all this to work properly (i.e. current directory should
contain the test directory, you should not be in the package directory
itself).
Also, the compiler will then properly find uncompiled .java files for
classes that you are using and compile them automatically.
So why don't they compile?
What am I doing wrong?
kind regards,
Luke

Cheers.
 
C

Chris Chilvers

[luke@knife test]$ javac PrefBean.java
PrefBean.java:8: cannot resolve symbol
symbol : class Pref
location: class test.PrefBean
Pref p = new Pref();
^
PrefBean.java:8: cannot resolve symbol
symbol : class Pref
location: class test.PrefBean
Pref p = new Pref();
^
2 errors
====================================
I can compile both classes if I don't include the 'package' statement
but I need to use these classes from JSPs, which only compile if the classes
are in packages,

So why don't they compile?
What am I doing wrong?

The problem is it looks for packages to be in that directly. Ie, anything in
the test package to be in test/blah.java or package test1.test2 to be in
test1/test2/blah.java
Thus PrefBean is expecting Pref.java to be in test/Pref.java

The solution is to go up one directory and use:
javac test/Pref.java
javac test/PrefBean.java
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top