How do I create and use a package?

Sorry for the noobish question, but I have never had the need to use a package and currently would like to learn how they work just to satisfy my own curiosity.

Suppose I have a file named Node.class in a folder named Classes, and I have another class named Main.class sitting in another folder. I want to create a Node object using the Main class.

How do I accomplish that? Would I need to use a package?

I tried to write package Classes; on my Main file but it did not work. thanks

[508 byte] By [Harmonicaa] at [2007-11-26 17:02:19]
# 1

This is an old explanation I wrote:

This is a minimal explanation of packages.

Assume that your programs are part of a package named myapp, which is specified by this first line in each source file:

package myapp;

Also assume that directory (C:\java\work\) is listed in the CLASSPATH list of directories.

Also assume that all your source files reside in this directory structure: C:\java\work\myapp\

Then a statement to compile your source file named aProgram.java is:

C:\java\work\>javac myapp\aProgram.java

And a statement to run the program is:

java myapp.aProgram

(This can be issued from any directory, as Java will search for the program, starting the search from the classpath directories.)

Explanation:

Compiling

A class is in a package if there is a package statement at the top of the class.

The source file needs to be in a subdirectory structure. The subdirectory structure must match the package statement. The top subdirectory must be in the classpath directory.

So, you generate a directory structure C:\java\work\myapp\ which is the [classpath directory + the package subdirectory structure], and place aProgram.java in it.

Then from the classpath directory (C:\java\work\) use the command: javac myapp\aProgram.java

Running

-

Compiling creates a file, aProgram.class in the myapp directory.

(The following is where people tend to get lost.)

The correct name now, as far as java is concerned, is the combination of package name and class name: myapp.aProgram (note I omit the .class) If you don't use this name, java will complain that it can't find the class.

To run a class that's NOT part of a package, you use the command: java SomeFile (assuming that SomeFile.class is in a directory that's listed in the classpath)

To run a class that IS part of a package, you use the command java myapp.aProgram (Note that this is analogous to the command for a class not in a package, you just use the fully qualified name)

ChuckBinga at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 2

I still can't do it.

Suppose I have a file named Node.java in the following folder C:\stuff

and I have a file named Main.java on my desktop

I want to create a Node object from the Main class.

Can you tell me exactly how I achieve this? Your previous explanation was not of much help. Sorry

Harmonicaa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 3
> Can you tell me exactly how I achieve this? Add the c:/stuff directory to your classpath (you've not indicated that Node is declared in the "stuff" package).~
yawmarka at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 4
Charles Bronson, is that you?%
duffymoa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 5
[url= http://java.sun.com/docs/books/tutorial/java/interpack/packages.html]Creating and Using Packages[/url]~
yawmarka at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 6

No the Java Tutorial is a waste of time. It makes problems more difficult to solve than they should be.

What do you mean "declare" stuff in Node? My Node.java file looks like this:

package stuff;

public class Node{

....

}

Harmonicaa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 7

> What do you mean "declare" stuff in Node? My

> Node.java file looks like this:

Fine. Add the c:/ path to your classpath (provided that's where stuff/Node.class is stored), and add "import stuff.Node;" to your Main class descriptor.

> No the Java Tutorial is a waste of time.

Then I'm not sure I'll be of much help. I doubt I can explain it better than they did. Who knows, though?

~

yawmarka at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 8
I went to environment variable and set the CLASSPATH to C:\Now at least my Main.java file compiles, but when I try to run it, I get a message saying "Exception in thread "main" java.lang.NoClassDefFoundError: Main
Harmonicaa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 9
Can someone please help me? I read the Java tutorial and I still can't do it. Someone please help
Harmonicaa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 10
> Can someone please help me? I read the Java tutorial> and I still can't do it. Someone please helpSod off you bloody lazy and stupid git.
PottedFearna at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 11

> Now at least my Main.java file compiles, but when I

> try to run it, I get a message saying "Exception in

> thread "main" java.lang.NoClassDefFoundError: Main

The Main class needs to be in the classpath, too. Note that the "." character signifies "the current directory".

~

yawmarka at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 12

> The Main class needs to be in the classpath, too.

> Note that the "." character signifies "the current

> directory".

>

> ~

How come noone mentioend this before? My CLASSPATH now looks like this: C:\;.

and everything works fine!!!!

(This wasn't mentioned in the Java tutorial BTW).

yawmark, you get 10 points :))))

Harmonicaa at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 13

[url=http://wiki.java.net/bin/view/Javapedia/ClassPath]Javapedia: Classpath[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html]How Classes are Found[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html]Setting the class path (Windows)[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html]Setting the class path (Solaris/Linux)[/url]

[url=http://www-106.ibm.com/developerworks/edu/j-dw-javaclass-i.html]Understanding the Java ClassLoader[/url]

java -cp .;<any other directories or jars> YourClassName

You get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

javac -classpath .;<any additional jar files or directories> YourClassName.java

You get a "cannot resolve symbol" message because the compiler can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

jverda at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 14

> > The Main class needs to be in the classpath, too.

> > Note that the "." character signifies "the current

> > directory".

> >

> > ~

>

> How come noone mentioend this before?

Maybe they thought you could read, think, figure out, experiment, etc. like the rest of us did.

jverda at 2007-7-8 23:30:05 > top of Java-index,Java Essentials,New To Java...
# 15

> > The Main class needs to be in the classpath, too.

> > Note that the "." character signifies "the current

> > directory".

> >

> > ~

>

> How come noone mentioend this before? My CLASSPATH

> now looks like this: C:\;.

>

> and everything works fine!!!!

>

> (This wasn't mentioned in the Java tutorial BTW).

>

Yes it is you sodding knobgobbler, in serveral places in fact.

Idiot.

PottedFearna at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 16

> > How come noone mentioend this before? My CLASSPATH

> > now looks like this: C:\;.

> >

> > and everything works fine!!!!

> >

> > (This wasn't mentioned in the Java tutorial BTW).

> >

>

> Yes it is you sodding knobgobbler, in serveral places

> in fact.

>

> Idiot.

Wow what a bitter person. It takes more than a written insult to devaluate my ego, but it saddens me to know that people like you exist. I feel sorry for you. Good luck.

Harmonicaa at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 17

> >

> > How come noone mentioend this before?

>

> Maybe they thought you could read, think, figure out,

> experiment, etc. like the rest of us did.

If I read the "package" Java Tutorial twice, and it never mentions that I need to append ;. to my classpath so as to get the whole package deal to work, then how am I supposed to know that? That's why I feel reading Java Tutorials is sometimes a waste of time.

Harmonicaa at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 18
> Charles Bronson, is that you?I think it is.
CaptainMorgan08a at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 19

> [url=http://wiki.java.net/bin/view/Javapedia/ClassPath

> ]Javapedia: Classpath[/url]

> [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/find

> ingclasses.html]How Classes are Found[/url]

> [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/wind

> ows/classpath.html]Setting the class path

> (Windows)[/url]

> [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/sola

> ris/classpath.html]Setting the class path

> (Solaris/Linux)[/url]

> [url=http://www-106.ibm.com/developerworks/edu/j-dw-ja

> vaclass-i.html]Understanding the Java

> ClassLoader[/url]

>

> java -cp .;<any other directories or jars>

> YourClassName

> You get a NoClassDefFoundError message because the

> JVM (Java Virtual Machine) can't find your class. The

> way to remedy this is to ensure that your class is

> included in the classpath. The example assumes that

> you are in the same directory as the class you're

> trying to run.

>

> javac -classpath .;<any additional jar files or

> directories> YourClassName.java

> You get a "cannot resolve symbol" message because the

> compiler can't find your class. The way to remedy

> this is to ensure that your class is included in the

> classpath. The example assumes that you are in the

> same directory as the class you're trying to run.

Thanks. I'll save those links for future reference.

Harmonicaa at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 20
> That's why I feel reading Java Tutorials is sometimes a waste of time.as are your posts Charles/Goldie.do you ever tire of being a WOFTAM?
Michael_Dunna at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...
# 21
http://java.sun.com/docs/books/tutorial/java/package/packages.html
kaniska at 2007-7-21 16:56:12 > top of Java-index,Java Essentials,New To Java...