• Post Reply Bookmark Topic Watch Topic
  • New Topic

Package Problem - Help Please  RSS feed

 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
I am facing a new problem in java.

I have written a class without any package structure lets call it as Test.java.

and i have one package called com\i2. I am writing a new class in that called
Hello.java

Sample Code is below
<<<<<<TEST.JAVA>>>>>>> This is in default Package

public class Test
{
public void add()
{
System.out.println("Addd!");
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}



<<<<<,HELLO.JAVA>>>.. this is in com\i2 package
package com.i2;

public class Hello
{public static void main(String[] args)
{
Test a = new Test();
a.add();
System.out.println("Hello World!");}}


i am getting error in the 3 line while compiling com\i2\Hello.java

I tried like this
javac -classpath D:\exam exam\com\i2\*.java

exam\com\i2\Hello.java:5: cannot resolve symbol
symbol : class Test
location: class com.i2.Hello
Test a = new Test();

I am keeping the Test.class in d:\exam and all my classpath are pointed to d:\exam

cant we refer the default package class from within a class under a package structure..
PLEASE HELP ME


i tried like this too
In Hello.java , i used import Test;

i get '.' expected import Test; error....

PLEASE TELL me a solution to the above
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do it. The compiler won't let you import classes from the default package into a file in a named package. This is done deliberately, to discourage the use of the default package.

If you desperately need to do this because Test is a class from a 3rd party library and you don't have the source, you could do it using reflection -- i.e., load the Class object using Class.forName(), create an instance with Class.newInstance() or Constructor.newInstance(), and call methods reflectively.
 
Karthik Rajendiran
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank you very much for ur kind reply
 
Herong Yang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is to write a wrapping class in a named package, and compile it with JDK 1.3, because this compilation rule was introduced in JDK 1.4.

--Herong
[ November 12, 2005: Message edited by: Herong Yang ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!