Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How to create a package in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create a package in Java?" Watch "How to create a package in Java?" New topic
Author

How to create a package in Java?

Karolin Franks
Greenhorn

Joined: Aug 24, 2005
Posts: 4
Hi, I'm slightly new in Java. I just want to know how to create a package in Java.
I have the following situation:
I have 2 classes in a folder named 'certification'.

package certification;
public class Parent
{
int x=9; //default access
}

AND

package certification;
class Child extends Parent
{
public static void main(String args[])
{
Parent p = new Parent();
p.Test();
}
public void Test()
{
System.out.println("var x is "+x);
}
}
But when I compile the class 'Child' in the "C:\certification" folder, it gives the error saying that 'cannot resolve symbol Parent'. What should I do?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
There are two ways to do this. Either compile all files ("javac *.java"), or move up one level in your directory hierarchy ("cd ..") and compile from there, i.e. "javac certification/Child.java".
By default, the compiler looks for missing classes according to their package. In your case, it wants certification/Parent, but since you're already in certification, it looks for c:/certification/certification/Parent, and that doesn't exist.


Ping & DNS - my free Android networking tools app
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I just want to know how to create a package in Java.

It sounds more like you want to know how to compile a class in a package in Java. You should compile from the root of the package. For example:

C:\>javac certification\Child.java

(I suggest that you don't create your packages with their root in the root directory. Something like C:\Java\certification\Child.java where the root of the package is in C:\Java would be better in my opinion)
[ August 24, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Karolin Franks
Greenhorn

Joined: Aug 24, 2005
Posts: 4
Hi Maryline, Dittmer,
Thank you so much. My problem solved!
Karolin Franks
Greenhorn

Joined: Aug 24, 2005
Posts: 4
Sorry Marilyn, for the spelling mistake in your name... thank u so much
Patrick van Zandbeek
Ranch Hand

Joined: Aug 17, 2005
Posts: 37
As a future tip, you can click the 4th icon in your post (white sheet of paper with pencil) to edit your posts if you make spelling mistakes or something.


u is a letter, not a word.
phil shea
Greenhorn

Joined: Aug 24, 2005
Posts: 12
I tried javac *.java. but it still shows the same error message.

phil
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
These path, classpath and package questions come up fairly often. See if THIS helps. Let me know if you spot anything that's not true.

I haven't done much command-line compiling on complex projects. Can anyone help Phil (and me) to compile all the java files in a directory tree?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
C:\>javac certification\*.java

I think that
C:\certification>javac *.java
will not work when there are multiple classes with package statements involved.
Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
yes it wont complile this way when the packages are inside.. in that package.


Ramesh Shanmugam - SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create a package in Java?
 
Similar Threads
doubt in access modifier. Please explain this code
Access protected member from the class of different package.
basic java question...please advice
Child extends Parent, Belongs to same package, Cannot access public variable
protected modifier