The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Here is your package...

Posted by Kishori Sharan on May 28, 2001 at 12:51 PM

Hello
You don't explicitly create a package. A package for a class just denotes that this class will be placed in a particular folder. Suppose your class definition is as follows.

/////////////Test.java starts here
package pack.spack ;
public class Test {
System.out.println ( "Hello" ) ;
}
/////////////Test.java ends here

We have mentioned that the package for the class Test is pack.spack. It means the class file Test.class ( Test.class will be created when you compile the Test.java file) will be placed in a folder ...\pack\spack . So, the Test.class' location will look like: ...\pack\spack\Test.class . If your pack had been myPackage then yout Test.class would have been expected to be in a folder like: ...\myPackage\Test.class. However, it doesn't say anything about the location of the folder pack. It could be anywhere on your system. So, you can create a folder and place your Test.class anywhere in you PC. Some of the possible location for your Test.class would be: c:\pack\spack\Test.class, c:\programs\pack\spack\Test.class or c:\java_programs\pack\spack\Test.class. The part before the pack folder in above path is taken from CLASSPATH environment variable you set on your PC. So, if your location of class file is c:\pack\spack\Test.class then your classpath should must include the folder c:\ as:
CLASSPATH = c:\; your_other_classpath_setting

You may not want to set ant classpath at all. In that case you need to pass classpath value when you want to run the class. When you put your class Test in pack.spack package then the fully qualified name of your class is pack.spack.Test and not just Test. So, at the time of running the class you need to mention this name . I suppose that you have placed your Test.java in c:\pack\spack\Test.java and you are running windows. Go to DOS prompt and type in following commands to compile and run it.
To compile it.
c:\pack\spack> javac Test.java

To run it
C:\pack\spack>java -classpath c:\ pack.spack.Test

To run the class you don't have to be in c:\pack\spack folder. You can run it from anywhere. To compile it if you are not in spack folder then you need to specify the full path as:
c:\abc> javac c:\pack\spack\Test.java

Thanks
Kishori





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us