wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java command question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java command question" Watch "java command question" New topic
Author

java command question

Steven Frank
Greenhorn

Joined: Feb 28, 2009
Posts: 2
I have doubts regarding this:

x-|
| - FindBaz.class
|
| - test -|
|-Baz.class
|
|-myApp -|
|- Baz.class
public class FindBaz{
public static void main(String [] args) {new Baz();)
}

In the test directory

public class Baz {
static { System.out.println("test/Baz")}
}
In the myApp directory
public class Baz {
static { System.out.println("myApp/Baz")}
}

The current directory is x.


I wonder why java FindBaz is incorrect statement.
Having no classpath specified on command line means that current directory and all its subderectories will be searched?
As a result Baz.class in the test directory is the first found entry so "test/Baz" will be printed. Where I am wrong?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Steven,

Welcome to the ranch!

The problem is that you are missing package and import statements.

First problem: When you want to put a class in a package, it is not enough to put the class under a given directory structure that reflects the name of the package, you need to add a package statement in the .java file. In your case, you need to add

to the top of your test/Baz.java file
and

to the top of your myApp/Baz.java file.

Second problem: When you have a reference to a class in a .java file, like you are refering to Baz inside your FindBaz.java file, you have two options: Import the package where the class is and simply refer to the class without package qualification (Baz,) or refer to the class using the full qualified name, including the package name (as in test.Baz.)

When you use a class name inside a .java file without qualification, and without adding an import statement, the class will be searched in the current package, so the class path won't help you. The class path will be used however during import statements.

Let me know if that doesn't clarify things.


All code in my posts, unless a source is explicitly mentioned, is my own.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

What is the source of this question ?

I am having a little difficulty understanding the directory hierarchy since the whitespace has been eaten up. But I ll give this a shot anyway.

Your class does not seem to belong to any given package. Which means that unless your current directory has this class and the classpath picks up classes from the current directory from which FindBazz runs, your code will not run correctly.

Either ensure that the class is available under the correct directory or make use of packages to explicitly specify which Baz you are looking for.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Steven Frank
Greenhorn

Joined: Feb 28, 2009
Posts: 2
Thank you both Ruben, Deepak.
I think i really got thanks to you.
I actually became confused because of this question which is wrong:
http://www.coderanch.com/t/270776/Programmer-Certification-SCJP/certification/K-B-SCJP-Chapter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java command question
 
Similar Threads
question about classpath
K&B Chapter 10 Self Help Question 9
problem in classpath and jar files and searching
compling files using classpath option
Compiling problem for Scjp