• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a package in Java?

 
Karolin Franks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Karolin Franks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maryline, Dittmer,
Thank you so much. My problem solved!
 
Karolin Franks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Marilyn, for the spelling mistake in your name... thank u so much
 
Patrick van Zandbeek
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
phil shea
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried javac *.java. but it still shows the same error message.

phil
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it wont complile this way when the packages are inside.. in that package.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic