Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Problems with packages

Thread view: 
zsomi - 10 Mar 2007 22:20 GMT
Hi all!

I'm newby in java.
I've the following problem:
I need to write some classes with the abstract Factory pattern.
I have written it in c++, but I cant in java. I tried this (It's very
simple):
//A.java
package up;

public abstract class A {
   public abstract String function(String s);
}
//AF.java
package up;

public class AF {

   private static A a;

   public static void main(String[] args) {

    AF af = new AF();
    A a2 = AF.getConcrete("C");

    String vmi = a2.function("ird ki");
    System.out.println(vmi);
   }

   public A getConcrete(String s) {
    if (s.compareTo("C") == 0 ) {
       a = new C();
    }
    return a;
   }
}

//C.java

package up;

public class C extends A {

   public String function(String s) {
    System.out.println(s);
    return s;
   }
}

So the problems:
If I dont put them into the package, it works fine.  If I put, then I
can compile it, but a cant run. I tried various ways, for example:
java AF, java -cp . AF , java up.AF, java -cp . up.AF
I tried to use .jar : jar cf uppkg.jar *.class
java -cp .:./uppkg.jar AF , java -cp .:./uppkg.jar up.AF
I got : java.lang.NoClassDefFoundError: up/AF (for every try)
WHY??? What did I wrong?
And my biggest problem is: I should use abstract factory with(becouse
of) different java toolkits.
So it would be better, if I can put the classes of different toolkit
into different packages and the abstract classes (with the factory
class) into another package.
For example:  ~/lib/Factory: A.class, AF.class -->package interfaces;
                      ~/lib/Factory/Dcm4che: B.class, C.class --
>package dcm4che;
                      ~/lib/Factory/Othertk: D.class, E.class -->
package othertk;
AND: my main should be in test.java in ~/apps/test
But I see, that it's not possible of the cross references. Am I right?
What can be a pretty solution of this problem?
Can anybody help me? Thanks a lot: Zsomi
Liz - 10 Mar 2007 22:43 GMT
<><
>So the problems:
>If I dont put them into the package, it works fine.  If I put, then I
[quoted text clipped - 4 lines]
>I got : java.lang.NoClassDefFoundError: up/AF (for every try)
>WHY??? What did I wrong?
<><

This may be a silly question but, here goes anyway.  Did you put the
classes which are in package up into a folder called up?  The folder
structure has to reflect the package structure, and I think you might
get the messages you have seen if the folder structure was wrong.
zsomi - 11 Mar 2007 00:48 GMT
> This may be a silly question but, here goes anyway.  Did you put the
> classes which are in package up into a folder called up?  The folder
> structure has to reflect the package structure, and I think you might
> get the messages you have seen if the folder structure was wrong.

Yes, my package name is the name of the conatining directory.   But it
doesnt work anyway:(
Tom Hawtin - 11 Mar 2007 01:17 GMT
> public abstract class A {
>     public abstract String function(String s);

You could use an interface.

>     AF af = new AF();
>     A a2 = AF.getConcrete("C");

That should be af.getConcrete("C")

>     public A getConcrete(String s) {
>     if (s.compareTo("C") == 0 ) {

s.equals("C") (or "C".equals(s) is more conventional. And better
variable names wouldn't go a miss.

> If I dont put them into the package, it works fine.  If I put, then I
> can compile it, but a cant run. I tried various ways, for example:

Are you sure you are compiling the code in the package, not the code in
the old location? With the correction, here's what I've got:

tackline@sun:~/scratch$ ~/sun/jdk1.6.0_01-amd/bin/javac up/*.java
tackline@sun:~/scratch$ ~/sun/jdk1.6.0_01-amd/bin/java up.AF
ird ki
ird ki
tackline@sun:~/scratch$ ls up
A.class  AF.class  AF.java  A.java  C.class  C.java

>                        ~/lib/Factory/Othertk: D.class, E.class -->
> package othertk;
> AND: my main should be in test.java in ~/apps/test
> But I see, that it's not possible of the cross references. Am I right?

I'm not exactly sure where you are going with this.

You can combine class files from multiple directory roots with
-classpath when you start Java. You can combine source files from
multiple source directory roots into one output directory with
-sourcepath. You can dynamically load code at runtime from runtime
specified locations using java.net.URLClassLoader.newInstance (although
it does get confusing - make sure you don't have the same class file
available through multiple paths/class loaders).

Tom Hawtin
zsomi - 11 Mar 2007 02:34 GMT
Hi Tom,

Thanks a lot for your answer. It works. But:

You (and then I) compile it one directory upper. So My .java files in
the "somtehing/up" directory, and if I compile and run it in the
"something" directory, it works. In the "up" dir, why doesnt it works?
Compiling is, but running isnt.

(I use "talking" variable and class names, but I works with big codes
and this way was easier to demonstrate my problem, and sorry for my
stupid mistake with AF)

With "cross references" I meant that
I want to put A and AF into one package, and want C into another. But
outside of a package I cant see C in class AF and cant see A in C. And
(I think) the package is created by compiling. So if I comiple C
first, I cant import there package of A and AF. And because of the
class hierachy and make system (my concrete) I cant compile them
together. (Maybe it could be help, but i'm not sure.) So thats my
problem. Maybe I'm silly (sorry), but I'm really newby in Java, I have
been working with c++ for a long time, so it's easier to me...

Thanks a lot once again!:)
Tom Hawtin - 11 Mar 2007 04:44 GMT
> You (and then I) compile it one directory upper. So My .java files in
> the "somtehing/up" directory, and if I compile and run it in the
> "something" directory, it works. In the "up" dir, why doesnt it works?
> Compiling is, but running isnt.

For compiling you are telling it which source files to compile. (In fact
it will work out dependent source files, and then it wont be able to
find those. javac is a bit slack. It even allows non-public classes to
be in files with completely 'incorrect' names.

For a JRE to find a class file it looks in particular locations for
particular files. For each directory in the classpath, it appends the
package names as directories and then class name plus ".class" as the
file name. If the class file is in the wrong directory or has the wrong
name, it wont be found.

> With "cross references" I meant that
> I want to put A and AF into one package, and want C into another. But
[quoted text clipped - 5 lines]
> problem. Maybe I'm silly (sorry), but I'm really newby in Java, I have
> been working with c++ for a long time, so it's easier to me...

If you can't compile the source files together, then you need to put the
previously compiled class files on the javac classpath.

So for instance:

src-a/a/A.java:
package a;
public class A { }

src-b/b/B.java:
package b;
class B extends a.A { }

javac               -d classes-a -sourcepath src-a src-a/a/A.java
javac -d classes-b -cp classes-a -sourcepath src-b src-b/b/B.java

Of course, you can't do that if they have cyclic dependencies.

Tom Hawtin
zsomi - 11 Mar 2007 12:48 GMT
Hi Tom,

thanks for the answer, that is what I wanted to know, I just didn't
wanted to spend a lot of time of solving an insolvable problem:) So,
now it's working within one package.

Zsomi


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.