wood burning stoves 2.0*
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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 TestPackage.java
import mypackage.*;
public class TestPackage
public static void main(String[] args)
MyClass myClassVar = new MyClass();
System.out.println("In the TestPackage Class");
in file MyClass.java
package mypackage;
public class MyClass
public MyClass()
System.out.println("My Class Constructor");
If I compile
D:\himanshu\trial>javac -d . *.java
.\MyClass.java:2: Class mypackage.MyClass already defined in MyClass.java.
public class MyClass
error: File .\MyClass.java does not contain type MyClass as expected. Please adjust the class path so that the file does not appear in the unnamed pac
TestPackage.java:6: Class MyClass not found.
MyClass myClassVar = new MyClass();
TestPackage.java:6: 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\MyClass.java So, to comile your code put the java file in the mypackage subdirectory and
C:\test>javac mypackage\MyClass.java
C:\test>javac TestPackage.java
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: http://aspose.com/file-tools
subject: basic Q.
Similar Threads
more on packages
Another CLASSPATH problem
K&B(SCJP5) page no:769.......
annotations : a little compilation problem
cannot find symbol error during compilation