• 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

basic Q.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
kage.
TestPackage.java:6: Class MyClass not found.
MyClass myClassVar = new MyClass();
^
TestPackage.java:6: Class MyClass not found.
MyClass myClassVar = new MyClass();
^
4 errors
XXXXXXXXXXX
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;
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic