Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > accessing packages cont'd

Reply
Thread Tools

accessing packages cont'd

 
 
Luke
Guest
Posts: n/a
 
      09-20-2003
Hi,

My second problem is this:

I have the following hierarachy:
t1/test/test2

then I have source files as follows:
t1/test/test2/Pref.java

t1/test/PrefBean.java

t1/UsePref.java

===========================
the files are like this:
Pref.java:
===========================
package test2;

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;
}
}
=============================
PrefBean.java
=============================
package test;

import test2.*;

public class PrefBean
{
// constructor method
public PrefBean()
{
Pref p = new Pref();
}
}
=============================
UsePref.java
=============================
package t1;

import test.*;
import test.test2.*;

public class UsePref
{
// constructor method
public UsePref()
{
PrefBean pb = new PrefBean();
Pref p2 = new Pref();
}
}
=============================
As you can see I want to access 'PrefBean' and 'Pref' from 'UsePref'.
However I try, I can't get it working.
I'm using java 1.4.2

Can someone help please!
Can you get this working on your machine

kind regards,
Luke
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2003
On Sat, 20 Sep 2003 16:09:09 +0900, "Luke" <(E-Mail Removed)>
wrote or quoted :

>package test2;


you can't do that. You have to keep all your package names relative
to the same base directory.

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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Luke
Guest
Posts: n/a
 
      09-20-2003
Hi,

I solved that prob, I believe

I changed each class's package directive to be:
for t1/test/test2/Pref.java: package t1.test.test2;
for t1/test/PrefBean.java: package t1.test;
for t1/UsePref.java: package t1;

and the import directives to
for t1/test/PrefBean.java:
import t1.test.test2.*;
for t1/UsePref.java:
import t1.test.test2.*;
import t1.test.*;

then I went to the parent directory of t1 and did:

javac -d . t1/*.java

this caused all the classes to compile.
and my computer to smile :-]

the article that helped was:
http://www.cs.wisc.edu/~hasti/cs368/.../Packages.html

I'll be posting my 3rd problem shortly.
Thanks for being a listening post,

kind regards,
Luke




> Hi,
>
> My second problem is this:
>
> I have the following hierarachy:
> t1/test/test2
>
> then I have source files as follows:
> t1/test/test2/Pref.java
>
> t1/test/PrefBean.java
>
> t1/UsePref.java
>
> ===========================
> the files are like this:
> Pref.java:
> ===========================
> package test2;
>
> 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;
> }
> }
> =============================
> PrefBean.java
> =============================
> package test;
>
> import test2.*;
>
> public class PrefBean
> {
> // constructor method
> public PrefBean()
> {
> Pref p = new Pref();
> }
> }
> =============================
> UsePref.java
> =============================
> package t1;
>
> import test.*;
> import test.test2.*;
>
> public class UsePref
> {
> // constructor method
> public UsePref()
> {
> PrefBean pb = new PrefBean();
> Pref p2 = new Pref();
> }
> }
> =============================
> As you can see I want to access 'PrefBean' and 'Pref' from 'UsePref'.
> However I try, I can't get it working.
> I'm using java 1.4.2
>
> Can someone help please!
> Can you get this working on your machine
>
> kind regards,
> Luke


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2003
On Sat, 20 Sep 2003 16:44:30 +0900, "Luke" <(E-Mail Removed)>
wrote or quoted :

>I changed each class's package directive to be:
>for t1/test/test2/Pref.java: package t1.test.test2;
>for t1/test/PrefBean.java: package t1.test;
>for t1/UsePref.java: package t1;


Normally you put more than one class in package, but you seem to have
grasped the naming conventions.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Packages : A loomingproblem? packages might no longer work? (well not on your platform or python version anyway) David Lyon Python 19 04-23-2009 11:10 PM
Incorporating data files into packages and accessing internally Todd Java 6 08-02-2007 03:20 PM
Advice on converting hashed packages to pseudo-hashed packages Ian Perl Misc 3 02-12-2005 12:17 AM
Do the javax.imageio packages replace packages in com.sun.image? Paul Smith Java 0 11-18-2003 02:58 PM



Advertisments