Error while importing user created packages

Hi ,

Complexity Novice

Problem Description

-

I am trying to run a simple package related program.

The package file is perfectly compiled and stored in the required path.

When I try to access this package from another file -- the import statement does not seem to be working.

It reports an error. However If I use the <pacakage>.<classname> it works perfectly fine.

I have the following files

1) The package file : Get.java

2) The package accessing file : Test.java

///////////////Get.java/////////////////////

package get;

publicclass Get

{

public Get()

{

}

public String iGet()

{

return("Hello World!");

}

}

/////////////////////Test.java/////////////////

import get.*;

class Test

{

publicstaticvoid main(String[] args)

{

Get g=new Get();//Here instead I use get.Get g= new get.Get() it works fine

System.out.println("Hello World!");

}

}

///////////////////End of Files////////////////

/////// Compiling/////////////

c:\packages>dir *.java//These are the only 2 files in the directory

10/05/2005 12:02 AM147 Test.java

10/04/2005 11:55 PM125 Get.java

c:\packages>javac -d . *.java

.\Get.java:3: duplicateclass: get.Get

publicclass Get

^

Test.java:7: cannot resolve symbol

symbol : constructor Get ()

location:class Get

Get g=new Get();

^

2 errors

I think the problem is clearly explained.

Thanks in advance

[2695 byte] By [shantanu_gga] at [2007-10-2 2:36:36]
# 1
You didn't store the Get class in a directory named as the package. Please read http://java.sun.com/docs/books/tutorial/java/interpack/createpkgs.html ...
MartinHilperta at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Hi Martin,

The command javac -d . *.java itselfs takes care of creating the directories.

Also I tried compiling only the Get.java file (just to be sure the package is created)

like javac -d . Get.java

and then javac -d . Test.java

Infact I even did compile Get.java as

javac Get.java

to get Get.class file

which I manually placed in the get directory ( also created manually)

Please try running these files at your end and sugggest a work around.

Thanks for your cooperation

shantanu_gga at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
A java file should be in the directory the same as the package!e.g.javac -d . get/Get.javaI sugget you try using an IDE like eclipse or netbeans. It will do this and so much else for you.
Peter-Lawreya at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4
Hi Peter,Can you clarify here,When the file Test.java is compiled all it needs is get/Get.classThere is no need for the file Get.java to be in get/Get.java
shantanu_gga at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
> There is no need for the file Get.java to be in> get/Get.javaOf course it is! Read the [url= http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#37546]Java Language Specification[/url]
MartinHilperta at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

> When the file Test.java is compiled all it needs is

> get/Get.class

>

> There is no need for the file Get.java to be in

> get/Get.java

I agree with these two statements and it is true that using -d will create the Get.class in the correct directory. However, it looks like you might have Get.java in the Classpath and you are using "import get.*;" in Test.java. You may be confusing the compiler because javac will automatically try to compile source code for dependent classes. I think it will work if you change the import to "import get.Get;"

atmguya at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
Hi atmguy, if import get.* cant work so cant import get.Getthe very concept of package fails if get.Get were to work.Please any one help me get a solution for thisRegards,Shantanu
shantanu_gga at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8
> Hi atmguy,> > if import get.* cant work so cant import get.Get> Did you try it?
atmguya at 2007-7-15 20:28:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...