• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java command question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Steven Frank
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both Ruben, Deepak.
I think i really got thanks to you.
I actually became confused because of this question which is wrong:
https://coderanch.com/t/270776/Programmer-Certification-SCJP/certification/K-B-SCJP-Chapter
 
reply
    Bookmark Topic Watch Topic
  • New Topic