Click to See Complete Forum and Search --> : getting error while compiling packages


vaishnavi
05-26-2009, 11:17 AM
Hi,

I am struggling a lot to compile a program which involves package. I am trying to learn this, but getting errors

Here is my first program:

package sun1;

public class packageConcept
{
public enum Color { RED, GREEN, BLUE }
public static void main(String args[])
{
System.out.println("hi");
}
}

I have placed the above code inside a folder called sun1 and named it as 'packageConcept.java'. I compiled using javac, went up one level and executed with java sun1.packageConcept.
It executed fine, and gave the output "hi".

My second program:

package beta;
import sun1.packageConcept;
import static sun1.packageConcept.*;
public class Beta {
packageConcept.Color g = GREEN;
public static void main( String[] argv)
{ System.out.println( GREEN); }
}

I placed the above program in a folder named 'beta' and name the file as Beta.java. When i tried to compile with javac Beta.java, I got the following errors:

package sun1 does not exist
import sun1.packageConcept

criterion9
05-26-2009, 12:52 PM
The sun1 folder would need to be in your classpath. Otherwise the javac compiler will not know where to find the classes.

vaishnavi
05-29-2009, 02:34 AM
This is the path of my sun1 folder:

C:\Documents and Settings\vaishnavi\Desktop\ifolder\sun1

My CLASSPATH environment variable is:

.;C:\Documents and Settings\vaishnavi\Desktop\ifolder\sun1

Still I am getting the same error :mad: :mad:

criterion9
05-29-2009, 08:45 AM
Try going up a level from the sun1 folder. The package is named sun1 so it will try to find the sun1 folder somewhere within the classpath.

vaishnavi
05-30-2009, 04:23 AM
I changed my CLASSPATH variable to

.;C:\Documents and Settings\vaishnavi\Desktop\ifolder

Yet not working

criterion9
06-01-2009, 01:32 PM
What IDE are you using or are you using the command line? If you are using an IDE there are ways to set the classpath in the appropriate settings. Did you reboot your computer after changing environment variables? (I'm assuming you are using XP and sometimes changes don't take effect until a reboot happens).

vaishnavi
06-02-2009, 07:49 AM
I am using command prompt and notepad editor. I am using XP. I dint reboot the system immediately after I changed the environment variable. But I now saw that the classpath is what i set previously. I compiled now. It is working fine as you told. Thanks.

So now I know how to compile a class inside a package. I am sure I will come up with many other doubts regarding this....

criterion9
06-02-2009, 10:44 AM
I ran into that same problem when I first started. The thing that really tripped me up was switching between an IDE and the command line. At the time I was starting out the documentation looked like it was written in Atlantian or some other such language. Just post on here if you get stuck again since usually someone on here has probably run into a similar if not identical issue.