File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java command question" Watch "java command question" New topic

java command question

Steven Frank

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

| - FindBaz.class
| - test -|
|-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/ file

to the top of your myApp/ file.

Second problem: When you have a reference to a class in a .java file, like you are refering to Baz inside your 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

Joined: Feb 24, 2006
Posts: 6662

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

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:
I agree. Here's the link:
subject: java command question
It's not a secret anymore!