• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package still not working

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I am facing problem in java packages

The directory structure is => work\pack1\pack2

The directory structure of packages are => \pack1\pack2

package pack2;
public class C
{
public C()
{
System.out.println("Inside C");
}

}
__________________________________________________________________________
As pack2 is in pack1 , i am trying to instantiate an object of class C , from pack1 , after compiling C.java
__________________________________________________________________________
package pack1;
import pack2.*;

public class B
{
public B()
{
System.out.println("Inside B");
}

public static void main(String arg[])
{
B b = new B();
C c = new C();
}
}
___________________________________________________________________________

There is no error in compilation of class B , but when i run the class B from one step up directory(from work ) like this

java pack1.B

it gives a runtime exeception

[ Exception in thread "main" java.lang.NoClassDefFoundError: pack2/C ]

___________________________________________________________________________

Please tell why is it happening and please give the solution . I am working on a Linux (Red hat) machine .

If it's a problem of setting CLASSPATH , please tell me how to set CLASSPATH

Regards
[ February 04, 2006: Message edited by: faisal usmani ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15355
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If pack2 is inside pack1, as your directory structure suggests, you should write:

package pack1.pack2;

in C.java, not "package pack2;", and in B.java you should write:

import pack1.pack2.*;

instead of "import pack2.*;". You should add the directory "work" to the classpath.

The JDK installation instructions explain how to set the classpath.
[ February 04, 2006: Message edited by: Jesper de Jong ]
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that , but it gives error

"package pack1.pack2 does not exist" and i think it is right since
B.java is itself in package pack1 then how can it import a package in which it is itself residing .


Hope you got my point

Regards
[ February 04, 2006: Message edited by: faisal usmani ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15355
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"package pack1.pack2 does not exist"

Did you put the directory named "work" in the classpath?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic