Forums Register Login

Java: Working With Packages

+Pie Number of slices to send: Send
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!

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
package com.bar;
import com.foo.X;

public class Y {


public static void main(String[] args)
{

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

}

}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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®.
1
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
 

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.






+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 753 times.
Similar Threads
Passing arrays to methods
Assertion Question
How to run a class outside JDeveloper IDE
How to make this packages example program work
How to turn on assert in TextPad ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:14:19.