• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package still not working

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"package pack1.pack2 does not exist"

Did you put the directory named "work" in the classpath?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic