File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Package still not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package still not working" Watch "Package still not working" New topic
Author

Package still not working

faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

"package pack1.pack2 does not exist"

Did you put the directory named "work" in the classpath?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package still not working
 
Similar Threads
packages and classpath! or at least i guess!
doubt in -cp option....
package problem
package trouble
Trouble with packages.