my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes basic Q. 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 "basic Q." Watch "basic Q." New topic

basic Q.


Joined: Jun 26, 2000
Posts: 25
I think I m missing some basic compiler dependent stuff :-)
in file
import mypackage.*;
public class TestPackage
public static void main(String[] args)
MyClass myClassVar = new MyClass();
System.out.println("In the TestPackage Class");
in file
package mypackage;
public class MyClass
public MyClass()
System.out.println("My Class Constructor");
If I compile
D:\himanshu\trial>javac -d . *.java
.\ Class mypackage.MyClass already defined in
public class MyClass
error: File .\ does not contain type MyClass as expected. Please adjust the class path so that the file does not appear in the unnamed pac
kage. Class MyClass not found.
MyClass myClassVar = new MyClass();
^ Class MyClass not found.
MyClass myClassVar = new MyClass();
4 errors
1.Can I know the reason?
2.the same code works fine if I declare TestPackage in a package "hisPackage".
3.the same code works if i change import statement in TestPackage to import mypackage.MyClass;
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Packages are actually a directory structure. Java uses packages at all times. The default package (no package specified) is the current working directory(basically). Since you have a package, it expects the code and the class file to be in a directory structure under the current working directory. ie if your current working directory is on a Win 32 box and is c:\test and the package you assigned to the new class is mypackage, the source code needs to be in c:\test\mypackage.
javac requires the fully qualified name of the class in order to comile it. The fully qualified name is the package\classname. Since you are on a Win 32 the fully qualified name is mypackage\ So, to comile your code put the java file in the mypackage subdirectory and
C:\test>javac mypackage\
C:\test>java TestPackage
My Class Constructor
In the TestPackage Class

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
I agree. Here's the link:
subject: basic Q.
It's not a secret anymore!