programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Java: Working With Packages

samarth mishra
Greenhorn
Posts: 13
My path variable: C:\Program Files\Java\jdk1.7.0_01\bin
My Java Programs are on E:\Java Programs
I manually created 2 folders inside the folder Java Programs foo and bar

And below are the classes that I put in the folders

__________________________________

__________________________________

Class X compiles fine but when I compile class Y it shows error that it cannot find X.

Kindly help!

Dave Tolls
Ranch Foreman
Posts: 3056
37
Show us the full command you are using to compile them, including the directory you are in.
Also, are those classes in a com/bar and com/foo folder?

Shyranish Sundararajan
Greenhorn
Posts: 4
package com.bar;
import com.foo.X;

public class Y {

public static void main(String[] args)
{

X x = new X();
x.apply(x.LOGICID);

}

}

Campbell Ritchie
Marshal
Posts: 56536
172
As Dave Tolls said, which folders are your classes in? If they are in the package com.bar then the bar folder must be inside a foo folder.

samarth mishra
Greenhorn
Posts: 13
Dave Tolls wrote:Show us the full command you are using to compile them, including the directory you are in.
Also, are those classes in a com/bar and com/foo folder?

Y.java has been saved to : E:\Java Programs\com\bar
X.java has been saved to : E:\Java Programs\com\foo

In the Command prompt the Command I am using is like:
E:\Java Programs\com\bar javac Y.java
And for the other class
E:\Java Programs\com\bar javac X.java

Campbell Ritchie
Marshal
Posts: 56536
172
you should compile X first because it is a dependency for Y.

I tried it with your code, only I commented back in the apply call and changed the int to final int:-
mkdir com
campbell@campbellsComputer:~/java$cd com campbell@campbellsComputer:~/java/com$ mkdir foo
campbell@campbellsComputer:~/java/com$mkdir bar campbell@campbellsComputer:~/java/com$ gedit bar/Y.java
campbell@campbellsComputer:~/java/com$gedit foo/X.java campbell@campbellsComputer:~/java/com$ cd ..
campbell@campbellsComputer:~/java$javac com/foo/X.java campbell@campbellsComputer:~/java$ gedit com/foo/X.java
campbell@campbellsComputer:~/java$javac com/foo/X.java campbell@campbellsComputer:~/java$ javac com/bar/Y.java
campbell@campbellsComputer:~/java$java com.bar.Y applied Apart from the fact that my Linux box has slightly different names for the folders you should find the same instructions should work on Windows®. Dave Tolls Ranch Foreman Posts: 3056 37 • 1 You need to give it the classpath. The easiest (IMO) is to simply compile from the E:\Java Programs directory (ie the source directory). javac -cp . com\bar\Y.java In fact, you won't need the "-cp ." as that is the default classpath, but if you have defined an global environment variable CLASSPATH (which you should not do) then you will need to include "-cp ." ... Does that make sense? ETA: And how long did it take me to finish writing that? Blimey... samarth mishra Greenhorn Posts: 13 Campbell Ritchie wrote:You should compile X first because it is a dependency for Y. I tried it with your code, only I commented back in the apply call and changed the int to final int:- mkdir com campbell@campbellsComputer:~/java$ cd com
campbell@campbellsComputer:~/java/com$mkdir foo campbell@campbellsComputer:~/java/com$ mkdir bar
campbell@campbellsComputer:~/java/com$gedit bar/Y.java campbell@campbellsComputer:~/java/com$ gedit foo/X.java
campbell@campbellsComputer:~/java/com$cd .. campbell@campbellsComputer:~/java$ javac com/foo/X.java
campbell@campbellsComputer:~/java$gedit com/foo/X.java campbell@campbellsComputer:~/java$ javac com/foo/X.java
campbell@campbellsComputer:~/java$javac com/bar/Y.java campbell@campbellsComputer:~/java$ java com.bar.Y
applied
Apart from the fact that my Linux box has slightly different names for the folders you should find the same instructions should work on Windows®.

It's Done! Thank you so much.

So, what was I doing wrong?
I was compiling X.java first and while compiling I went into that folder

And, when I needed to compile X.java I did cd foo
(Compiled Successfully)
Similarly, (Not Compiled) - 4 Errors : Can't find symbol X,com.foo package doesn't exist and 2 more.

Campbell Ritchie
Marshal
Posts: 56536
172
Don't know. Notice that I changed back to the directory one “above” com before trying to compile anything. You probably need to go back to that folder before it will recognise the path for your com.foo package. Or you can try adding it with the -cp option which DT has told you about.

samarth mishra
Greenhorn
Posts: 13
Dave Tolls wrote:You need to give it the classpath.
The easiest (IMO) is to simply compile from the E:\Java Programs directory (ie the source directory).
javac -cp . com\bar\Y.java

In fact, you won't need the "-cp ." as that is the default classpath, but if you have defined an global environment variable CLASSPATH (which you should not do) then you will need to include "-cp ." ...

Does that make sense?

ETA: And how long did it take me to finish writing that?
Blimey...

Thanks Dave!
I get it that if I want to refer a class, I have to be a level above it.
Is this the rule behind it?
And, about the classpath thing, I never really understood the concept completely, so if you can help me understand, what is it actually that we sometimes set in Environoment variables with the variable name as classpath.
And, the -cp thing. It's been some time I am doing Java Programming but, never really understood the concept behind it.

Campbell Ritchie
Marshal
Posts: 56536
172
samarth mishra wrote: . . .
Thanks Dave!
DT did give you the answer before I did.
. . . I have to be a level above it. . . .
You actually have to be at a level where you can write com\foo\X.*** and find the file you want. If the package name is com.foo then you have to be able to find that class in the folder com\foo. The -cp option means classpath. The classpath variable tells the compiler and the JVM where to find other dependencies. Don't set a system classpath.

Dave Tolls
Ranch Foreman
Posts: 3056
37
You could compile inside the package directory, but I find it simpler to view those directories as part of the class name.
In essence where you run javac from is considered to be your classpath (ie a location to look for relevant classes) unless you supply a list using -cp.
In your case, Y.java required com.foo.X.java...so the compiler looked in <current directory>\com\foo for X.java.

 Don't get me started about those stupid light bulbs.