Re: a little confused about packages...




<jon23d@xxxxxxxxx> wrote...

javac TestPkg.java <--- this works fine
java TestPkg <---- this doesn't!

This is what I've used up until I started using packages,
and has worked fine...

When you use packages, the package name is a part of the full class name.

Let's say that you are in the directory

~/devel/java/TestPkg/

....when you compile with

javac TestPkg.java

This will create the following class file:

~/devel/java/TestPkg/TestPkg.class

Which means that you will have to move up one directory:

~/devel/java/

....to be able to execute it with:

java TestPkg.TestPkg

This is because the packages in Java are used for both logical and physical
structures.

The package name is in Java also a designator of the physical structure, how
to organize classes on the disk or in a jar, but as they are foremost a
designator in order to organize them logically according to their use, you
need the full name (incl. package name), when using them.

That is why you use "import" for some classes in other packages (the logical
organization), but might also need -cp in order to find them (the physical
organization).

When you compile and execute from within an IDE, you should be especially
careful, as most of such IDEs also creates the directory structure when
finding out that a class is within a package.

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
.



Relevant Pages

  • A whole host of software problems :-)
    ... I've had some problems with CSW packages since I tried to ... first of all cleaned out all of my CSW installations, ... older versions of GIMP and Mozilla from unofficial mirror sites. ... insisted on updating Java, and I think that was when the problems ...
    (comp.unix.solaris)
  • Re: Help with -classpath and packages
    ... Java has packages and classes, ... I know what the generic terminology means, but I don't know what the IDE means when it uses those words. ... It seems to me there would be no easy to way to compile the "applications" and "libraries" with just the simple javac command-line compiler once they are split up into a whole bunch of isolated directory trees. ...
    (comp.lang.java)
  • Re: Module clarification
    ... I'm a Java guy and I'm playing around Python these days... ... we organize our classes into packages and then jarring the ... because the namespace is tied to ...
    (comp.lang.python)
  • SUMMARY: Missing Java system packages on Solaris 9
    ... David Foster gave me this here listing to bring Java back to life. ... these are the Solaris Java system packages, ...
    (SunManagers)
  • Re: GNU Java packages
    ... Java libraries. ... There're always the "standard" packages ... am I sure that the application will work with Sun VM too? ... Because if I use the gnu version the computer where I will install the ...
    (comp.lang.java.programmer)