This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package name can not be separated from class, . with -classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "package name can not be separated from class, . with -classpath" Watch "package name can not be separated from class, . with -classpath" New topic
Author

package name can not be separated from class, . with -classpath

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 178

package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

does it because as A is in package top it can only be searched from above the top directory


One more problem is :
the package top is in directory Desktop
so when i try this to compile A.java from directory desktop
javac -classpath top A.java
it says A.java can not found , classpath specifies its in top package then it should have compiled A.java

Last one:
Does specifying current directory (.) is must with -classpath option ,otherwise it doesn`t look into current directory

An answer that i can understand is required


Mahtab Alam (OCJP )
Blogs : alamsql.blogspot.in , alammahtab.blogspot.in
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

They are in the same directory then why do you need a classpath??
Top is the folder which has the java files.
first compile A.java and then B.java (keep you current dir as top and no need a classpath for that)

yes the (.) is required to find the files in the current directory. and there should be a semicolon after the "package top" in B.java


OCPJP
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 610
Mahtab Alam wrote:package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

does it because as A is in package top it can only be searched from above the top directory


One more problem is :
the package top is in directory Desktop
so when i try this to compile A.java from directory desktop
javac -classpath top A.java
it says A.java can not found , classpath specifies its in top package then it should have compiled A.java



To compile A.java in your current desktop directory : javac top/A.java
To compile B.java in your current desktop directory javac top/B.java

To compile A.java in your current top directory : javac A.java
To compile B.java in your current top directory javac B.java
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 178

Thanks for reply ,
I know those ways to compile .
I want to know why it doesn`t work with classpath

Because exam uses classpath option .and you know choose all that apply


What i want to know basically is
Say you are in directory A in which file run.class is ,now lets say we have jar file my.jar inside A directory .To run A.class it needs access to some class under my.jar
java -classpath my.jar A

or to find the file A.class under current directory i have to use a . also
so
java -classpath .:my.jar A

just thats it
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 610
package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

Since you are in top directory, javac can find B.java, but it cannot find top.A
This is the most confusing part. Under top directory, there is an A class. Under desktop directory, there is a top.A class

In javac -classpath . B.java, javac first looks for top.A class in order to compile B. But under top directory, there is an A.class , not top.A.

-classpath should include the root of the class you are looking for.
Suppose you are under destkop directory and you are looking for top. A class, then javac -classpath . B.java works.
Suppose you are under top directory, your command should be javac -classpath ..\ B.java
It says going back to one level up to find top. A
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package name can not be separated from class, . with -classpath
 
Similar Threads
Classpath Query
K&B Chapter 10, Self test doubt
Classpath doubt k&b chp 10 que 5
using classpath with javac Problem
how javac search file?