The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000



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.


Put your class in a package...

Posted by Kishori Sharan on December 14, 2000 at 4:33 PM

When you campile a java program then you have to specify the .java file name which contains the java code. So, here you can specify full path of the .java file which enables you to compile a java prpgram in a folder from another folder. But, when you want to run a java program then you have to specify the class name of the class you want to run. Lets take an example
public class Hello {
public static void main ( String[] args ) {

System.out.println ( "Hello Java!!!" ) ;


Suppose you have saved this file as
C:\kk\java\ and now your current folder is
c:\kk from which you want to run Hello class.
Whether or not you can run Hello class from c:\kk which is in c:\kk\java\Hello.class depends on the class path setting and the package if have declared for Hello class. Like files you can also specify full class name or simple class name. If you defined a class and don't put it in any package then its simple and full names are the same as the name of the class. If there is no package defined for a class then it is put in unnamed package and in most of the platform the current directory is considered as the unnamed package. So if you give a comman
c:\kk> java Hello
then you are specifying the simple name of the class i.e. Hello and java will consider this Hello class in unnamed package i.e. in current directory which it won't find.
If you specify a package for the class Hello as
package ;
then the simple name of your class is Hello and its full name is So you can run this class from anywhere specifying its full name as
provided your class path setting is correct.
So put your class in a package and then run your class from any directory using its full name.



Sponsored Links

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