You have to add the package statement to Demo.java, compile the file again and import statement to class Test.java to import the Demo class, because you're trying to use Demo class and there's no way to know for the compiler where that class is then.

Command line invocation depends on in which directory you are, but you can use absolute path - putting / sing at the begining of the path, so it wouldn't matter where you are.

The command line invocation would be then:

javac -cp / /Test/a/test.java

javac -cp - that's abvious

/ - this is classpath, in order to find a class (Demo.class) that is in a package (Test) you have to include in your classpath a directory that is a superdirectory of package's root directory, in our case package's root directory is "Test" and according to your description "Test's" superdirectory is system root, so we need only "/"

/Test/a/test.java - path to your source file

Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83

posted Mar 17, 2006 02:58:00

0

Thanks !Aleksander Zielinski ,you are right and your English is well

You have to add the package statement to Demo.java, compile the file again and import statement to class Test.java to import the Demo class, because you're trying to use Demo class and there's no way to know for the compiler where that class is then.

yor meaning is we can NOT import default package's members(eg: class) in java packages that is say: In user-defined packages we can NOT use the class or it's members which is declared in default package [ March 17, 2006: Message edited by: Changchun Wang ]

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127

posted Mar 17, 2006 03:17:00

0

Thank you.

If you ask if there have to be import and package statements, the answer is no. I just thought it would be more "proffesionall" to use directory structure packages

If you remove import and package statements from source files, then for example, if we are in directory "a" the command line invocation would be:

Lets take the original question into consideration. Do we have to put Demo.java in a package. Can't we do w/o that. I tried it but I was unable to compile Test.java as it could not find Demo.class.

My directory structure is

/vijay/java/Demo.java /vijay/java/Demo.class /vijay/java/Test.java

I am compiling Test.java from the directory /vijay/java using). I have the current directory (.) in my classpath. But still, why can't the java compiler find Demo.class.

regards, vijay.

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127

posted Mar 17, 2006 04:08:00

0

You used -d option which tells the compiler where to put class files, but regardless of that you should be able to compile, because if you have all files in one directory "java" and used command line invocation:

javac -d . Test.java

that means that compiler should put all classes to the current directory and that in our case doesn't really matter.

According to your previous post... [QUOTEIf you ask if there have to be import and package statements, the answer is no. I just thought it would be more "proffesionall" to use directory structure packages Do you mean that classes located in default package can be used only by other classes also located in default package ?

If a given class from within a package try to access another class located in default package, will I always get compile errors ?

I'm really confused about that. Could you please explain ?

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127

posted Mar 17, 2006 09:36:00

0

Classes located in default package cannot be imported, default package can be used only from other classes in default package. That's why one should aviod using default package in professional programming.

By the way, Edisandro I sent you a private message yesterday, please read it.