• 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 Question

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

I have a problem compiling the TestPak2.java file.Here is the code



and


the command that is used to invoke the compiler is


D:\JAVA\shekhar java\sudiptoNew\pak> javac TestPak2.java



The error message that i got is



As both the .java files are in the same package,isn't it that TestPak2.java supposed to find the pak.TestPak.java file?
Please help and explain....
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its working brother.there may be some problem in your compiler or editor.check that.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bro...I guess it should have something to do with the following 2 concepts: classpath configuration and packaging a class

first, you need to set the classpath appropriately, for example you can set the classpath to the current folder by specifying it at command line, using this flag

-cp .


if you do this, then everytime you refer to a class in your source file , the compiler and JVM will look for that class at the current folder and if they couldn't find it, then there will be an error. But, a BIG POINT here, which I guess is the main problem that causes the compiler error in your situation, is that if that class is packaged in a package (such as package "pak"), then compiler and JVM will look for that class at the subfolder "pak" of the folder specified at classpath (in this case the current folder)

now, back to your case as an example. At pakTest2.java, you declare a local variable:

pak.TestPak tp=new pak.TestPak()

inside the main method. The compiler assumes that TestPak class is packaged to a package "pak" (since you declare it as pak.TesPak) and it needs to know where to find that class and it uses the Classpath variable to find it. If you set the classpath variable to the current folder and call javac command from the folder:

D:\JAVA\shekhar java\sudiptoNew\pak\javac -cp . TestPak2.java

then the compiler will look for TestPak class at the subfolder "pak" of the folder "pak", i.e. the compiler look for TestPak class at the folder

D:\JAVA\shekhar java\sudiptoNew\pak\pak

which I assume isn't available and forces the compiler to generate errors saying that the class "TestPak" couldn't be found.

You can solve your problem in several ways, for example :
1. invoke javac command from the folder D:\JAVA\shekhar java\sudiptoNew and set the classpath to the current folder, i.e:

D:\JAVA\shekhar java\sudiptoNew\javac -cp . pak\TestPak2.java


2. invoke javac command from the folder D:\JAVA\shekhar java\sudiptoNew\pak and set the classpath to the super folder, i.e.:

D:\JAVA\shekhar java\sudiptoNew\pak\javac -cp .. TestPak2.java



well..obviously the 2 ways above aren't the only ways..you can do some combinations on classpath variable and referring to the source file TestPak.java by also specifying its path.

Hope this helps....


Khomeini
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:


D:\JAVA\shekhar java\sudiptoNew>javac pak\TestPak2.java

I'm supposing that you named your file TestPak2.java

 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Khomeini ,Leanardo and Avi. I got it now. I was compiling it from the wrong place. To compile TestPak2.java I had to go one folder up and give the command javac.
Thanks for all your help. LOL
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:try this:


D:\JAVA\shekhar java\sudiptoNew>javac pak\TestPak2.java

I'm supposing that you named your file TestPak2.java



This worked. I went nuts before trying this...
 
reply
    Bookmark Topic Watch Topic
  • New Topic